如何使用 SAM 正确创建 Dynamodb table
How to properly create a Dynamodb table with SAM
刚刚开始使用 AWS SAM。
想使用 Lambda 和 DynamoDB 创建一个 CRUD 微服务
我的 template.yaml:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
A simple backend (read/write to DynamoDB) with a RESTful API endpoint using Amazon API Gateway.
Resources:
microservicehttpendpoint:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs12.x
CodeUri: .
Description: >-
A simple backend (read/write to DynamoDB) with a RESTful API endpoint using Amazon API Gateway.
MemorySize: 512
Timeout: 10
Policies:
- DynamoDBCrudPolicy:
TableName: MyTable
Events:
Api1:
Type: Api
Properties:
Path: /MyResource
Method: ANY
MyTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey: name
TableName: dynocrud-table
我希望创建三个资源:a) Lambda,b) Api 网关端点,3) DynamoDB table
Lambda 和 API 网关端点正在按预期创建,但没有 DynamoDB table,也没有错误。
这是 cli 输出:
sam deploy
Uploading to dyno-crud/5c01af22215fe32fe7f18fa00be 5706 / 5706 (100.00%)
Deploying with following values
===============================
Stack name : dyno-crud
Region : us-east-1
Confirm changeset : True
Disable rollback : False
Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-141rt5mvjki0m
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {"TableNameParameter": "dynocrud-table"}
Signing Profiles : {}
Initiating deployment
=====================
Uploading to dyno-crud/d085f2bf53ae0ab235eabed4814.template 897 / 897 (100.00%)
Waiting for changeset to be created..
CloudFormation stack changeset
-----------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-----------------------------------------------------------------------------------------------------------------
+ Add ServerlessRestApiDeploymen AWS::ApiGateway::Deploymen N/A
t1fee5cc247 t
+ Add ServerlessRestApiProdStage AWS::ApiGateway::Stage N/A
+ Add ServerlessRestApi AWS::ApiGateway::RestApi N/A
+ Add microservicehttpendpointAp AWS::Lambda::Permission N/A
i1PermissionProd
+ Add microservicehttpendpointRo AWS::IAM::Role N/A
le
+ Add microservicehttpendpoint AWS::Lambda::Function N/A
-----------------------------------------------------------------------------------------------------------------
Changeset created successfully. arn:aws:cloudformation:us-east-1:458815622243:changeSet/samcli-deploy1639025652/bcbadc92-6106-4cb4-b551-54f3b9774936
Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y
2021-12-08 22:54:43 - Waiting for stack create/update to complete
CloudFormation events from stack operations
-----------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-----------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS AWS::IAM::Role microservicehttpendpointRo -
le
CREATE_IN_PROGRESS AWS::IAM::Role microservicehttpendpointRo Resource creation
le Initiated
CREATE_COMPLETE AWS::IAM::Role microservicehttpendpointRo -
le
CREATE_IN_PROGRESS AWS::Lambda::Function microservicehttpendpoint -
CREATE_IN_PROGRESS AWS::Lambda::Function microservicehttpendpoint Resource creation
Initiated
CREATE_COMPLETE AWS::Lambda::Function microservicehttpendpoint -
CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi -
CREATE_COMPLETE AWS::ApiGateway::RestApi ServerlessRestApi -
CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi Resource creation
Initiated
CREATE_IN_PROGRESS AWS::Lambda::Permission microservicehttpendpointAp -
i1PermissionProd
CREATE_IN_PROGRESS AWS::Lambda::Permission microservicehttpendpointAp Resource creation
i1PermissionProd Initiated
CREATE_IN_PROGRESS AWS::ApiGateway::Deploymen ServerlessRestApiDeploymen -
t t1fee5cc247
CREATE_IN_PROGRESS AWS::ApiGateway::Deploymen ServerlessRestApiDeploymen Resource creation
t t1fee5cc247 Initiated
CREATE_COMPLETE AWS::ApiGateway::Deploymen ServerlessRestApiDeploymen -
t t1fee5cc247
CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage -
CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage Resource creation
Initiated
CREATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdStage -
CREATE_COMPLETE AWS::Lambda::Permission microservicehttpendpointAp -
i1PermissionProd
CREATE_COMPLETE AWS::CloudFormation::Stack dyno-crud -
-----------------------------------------------------------------------------------------------------------------
Successfully created/updated stack - dyno-crud in us-east-1
为了与其他资源一起创建 DynamoDB table,我应该更改什么?
请确保您的 yaml 文件正确缩进。 Type:
应在资源名称下方缩进,例如
MyTable:
Type: AWS::Serverless::SimpleTable <-- indented here
Properties:
PrimaryKey: name
TableName: dynocrud-table
刚刚开始使用 AWS SAM。 想使用 Lambda 和 DynamoDB 创建一个 CRUD 微服务 我的 template.yaml:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
A simple backend (read/write to DynamoDB) with a RESTful API endpoint using Amazon API Gateway.
Resources:
microservicehttpendpoint:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs12.x
CodeUri: .
Description: >-
A simple backend (read/write to DynamoDB) with a RESTful API endpoint using Amazon API Gateway.
MemorySize: 512
Timeout: 10
Policies:
- DynamoDBCrudPolicy:
TableName: MyTable
Events:
Api1:
Type: Api
Properties:
Path: /MyResource
Method: ANY
MyTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey: name
TableName: dynocrud-table
我希望创建三个资源:a) Lambda,b) Api 网关端点,3) DynamoDB table Lambda 和 API 网关端点正在按预期创建,但没有 DynamoDB table,也没有错误。 这是 cli 输出:
sam deploy
Uploading to dyno-crud/5c01af22215fe32fe7f18fa00be 5706 / 5706 (100.00%)
Deploying with following values
===============================
Stack name : dyno-crud
Region : us-east-1
Confirm changeset : True
Disable rollback : False
Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-141rt5mvjki0m
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {"TableNameParameter": "dynocrud-table"}
Signing Profiles : {}
Initiating deployment
=====================
Uploading to dyno-crud/d085f2bf53ae0ab235eabed4814.template 897 / 897 (100.00%)
Waiting for changeset to be created..
CloudFormation stack changeset
-----------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-----------------------------------------------------------------------------------------------------------------
+ Add ServerlessRestApiDeploymen AWS::ApiGateway::Deploymen N/A
t1fee5cc247 t
+ Add ServerlessRestApiProdStage AWS::ApiGateway::Stage N/A
+ Add ServerlessRestApi AWS::ApiGateway::RestApi N/A
+ Add microservicehttpendpointAp AWS::Lambda::Permission N/A
i1PermissionProd
+ Add microservicehttpendpointRo AWS::IAM::Role N/A
le
+ Add microservicehttpendpoint AWS::Lambda::Function N/A
-----------------------------------------------------------------------------------------------------------------
Changeset created successfully. arn:aws:cloudformation:us-east-1:458815622243:changeSet/samcli-deploy1639025652/bcbadc92-6106-4cb4-b551-54f3b9774936
Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y
2021-12-08 22:54:43 - Waiting for stack create/update to complete
CloudFormation events from stack operations
-----------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-----------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS AWS::IAM::Role microservicehttpendpointRo -
le
CREATE_IN_PROGRESS AWS::IAM::Role microservicehttpendpointRo Resource creation
le Initiated
CREATE_COMPLETE AWS::IAM::Role microservicehttpendpointRo -
le
CREATE_IN_PROGRESS AWS::Lambda::Function microservicehttpendpoint -
CREATE_IN_PROGRESS AWS::Lambda::Function microservicehttpendpoint Resource creation
Initiated
CREATE_COMPLETE AWS::Lambda::Function microservicehttpendpoint -
CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi -
CREATE_COMPLETE AWS::ApiGateway::RestApi ServerlessRestApi -
CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi Resource creation
Initiated
CREATE_IN_PROGRESS AWS::Lambda::Permission microservicehttpendpointAp -
i1PermissionProd
CREATE_IN_PROGRESS AWS::Lambda::Permission microservicehttpendpointAp Resource creation
i1PermissionProd Initiated
CREATE_IN_PROGRESS AWS::ApiGateway::Deploymen ServerlessRestApiDeploymen -
t t1fee5cc247
CREATE_IN_PROGRESS AWS::ApiGateway::Deploymen ServerlessRestApiDeploymen Resource creation
t t1fee5cc247 Initiated
CREATE_COMPLETE AWS::ApiGateway::Deploymen ServerlessRestApiDeploymen -
t t1fee5cc247
CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage -
CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage Resource creation
Initiated
CREATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdStage -
CREATE_COMPLETE AWS::Lambda::Permission microservicehttpendpointAp -
i1PermissionProd
CREATE_COMPLETE AWS::CloudFormation::Stack dyno-crud -
-----------------------------------------------------------------------------------------------------------------
Successfully created/updated stack - dyno-crud in us-east-1
为了与其他资源一起创建 DynamoDB table,我应该更改什么?
请确保您的 yaml 文件正确缩进。 Type:
应在资源名称下方缩进,例如
MyTable:
Type: AWS::Serverless::SimpleTable <-- indented here
Properties:
PrimaryKey: name
TableName: dynocrud-table