amazon-web-services - 如何在 AWS CloudFormationTemplate 中为 Lambda 引用 AWS GlobalSecondaryIndex 名称
问题描述
我正在尝试编写一个云形成模板,其中 DynamoDBTable 的全局二级索引 (GSI) 名称可用作 AWS Lambda 中的环境变量。我有下面的模板,但无法在 AWS Lambda 中获取对 GSI 名称的引用(模板中的最后一行)。
在此先感谢您的帮助。
# 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
解决方案
GSI 正是您所命名的,因此您只需传入customer-index-1
推荐阅读
- javascript - 如何在选项卡关闭或窗口关闭时始终发出警报
- docker - 在 Docker 中为 x86_64 机器上的 Raspberry Pi Zero 构建 Python 轮子
- amazon-web-services - 使用 AWS 保护视频链接
- java - OpenCSV 在解析时抛出错误捕获 CSV 标头
- nestjs - Nest js 打包挂在无服务器框架的构建过程中
- react-native-render-html - 不尊重 fontSize 的元素
- python - 如何重新启动使用套接字的 python 程序?
- emacs - 组织模式 | 如何将一列与另一列相乘并对结果求和?
- stm32 - STM32F4通过UART写内部flash
- python - 图像和帖子仅适用于 Django Admin 而不是常规形式