Как ссылаться на AWS имя GlobalSecondaryIndex в AWS CloudFormationTemplate для лямбды - PullRequest
0 голосов
/ 16 февраля 2020

Я пытаюсь написать шаблон формирования облака, в котором имя Globaloary Secondary Index (GSI) для DynamoDBTable доступно в качестве переменной среды в AWS Lambda. У меня есть шаблон ниже, и я не могу получить ссылку на имя GSI в AWS лямбда (последняя строка в шаблоне).

Заранее благодарим за помощь.

# The AWSTemplateFormatVersion identifies the capabilities of the template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
AWSTemplateFormatVersion: 2010-09-09
Description: >-
  temp

Parameters:
  Environment:
    Type: String
    Description: The name of the stage, e.g. "dev", "preprod", "prod"
    Default: ravi

Transform:
  - AWS::Serverless-2016-10-31

Resources:
  customerLambdaDLQ:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub "${Environment}-customer-lambda-dead-letter-queue"
  projectionsLambdaRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - "sts:AssumeRole"
      Path: /
      Policies:
        - PolicyName: !Sub "${Environment}-customer-lambda-role"
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - "sqs:SendMessage*"
                  - "dynamodb:GetItem"
                  - "dynamodb:DeleteItem"
                  - "dynamodb:PutItem"
                  - "dynamodb:Scan"
                  - "dynamodb:Query"
                  - "dynamodb:UpdateItem"
                  - "dynamodb:BatchWriteItem"
                  - "dynamodb:BatchGetItem"
                  - "dynamodb:DescribeTable"
                  - "dynamodb:ConditionCheckItem"
                  - "dynamodb:DescribeStream"
                  - "dynamodb:GetRecords"
                  - "dynamodb:GetShardIterator"
                  - "dynamodb:ListStreams"
                Resource:
                  - !GetAtt customerLambdaDLQ.Arn
  customerTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Sub "${Environment}-customer-table"
      AttributeDefinitions:
        - AttributeName: PK
          AttributeType: S
        - AttributeName: SK
          AttributeType: S
        - AttributeName: indexKey_1
          AttributeType: S
        - AttributeName: indexKey_2
          AttributeType: S
        - AttributeName: customerId
          AttributeType: S
      KeySchema:
        - AttributeName: PK
          KeyType: HASH
        - AttributeName: SK
          KeyType: RANGE
      GlobalSecondaryIndexes:
        - IndexName: customer-index-1
          KeySchema:
            - AttributeName: indexKey_1
              KeyType: HASH
            - AttributeName: indexKey_2
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
        - IndexName: customer-index-2
          KeySchema:
            - AttributeName: customerId
              KeyType: HASH
            - AttributeName: indexKey_2
              KeyType: RANGE
          Projection:
            NonKeyAttributes:
              - first_name
              - last_name
              - city
              - state
              - country
            ProjectionType: INCLUDE
      BillingMode: PAY_PER_REQUEST
      SSESpecification:
        SSEEnabled: true
  primer:
    Type: AWS::Lambda::Function
    Properties:
      Runtime: nodejs12.x
      Role: !GetAtt projectionsLambdaRole.Arn
      Handler: index.handler
      Code:
        ZipFile: |
          var aws = require('aws-sdk')
          var response = require('cfn-response')
          exports.handler = function(event, context) {
              console.log("REQUEST RECEIVED:\n" + JSON.stringify(event))
              // For Delete requests, immediately send a SUCCESS response.
              if (event.RequestType == "Delete") {
                  response.send(event, context, "SUCCESS")
                  return
              }
              var responseStatus = "FAILED"
              var responseData = {}
              var functionName = event.ResourceProperties.FunctionName
              var lambda = new aws.Lambda()
              lambda.invoke({ FunctionName: functionName }, function(err, invokeResult) {
                  if (err) {
                      responseData = {Error: "Invoke call failed"}
                      console.log(responseData.Error + ":\n", err)
                  }
                  else responseStatus = "SUCCESS"
                  response.send(event, context, responseStatus, responseData)
              })
          }
      Description: Invoke a function during stack creation.
      Environment:
        Variables:
          TABLE_NAME: !Ref customerTable
          GSI_1_NAME: !GetAtt customerTable.GlobalSecondaryIndexes.customer-index-1

1 Ответ

0 голосов
/ 16 февраля 2020

GSI - это именно то, что вы назвали, поэтому вы можете просто ввести customer-index-1

...