要理解在AWS SAM(Serverless Application Model)代码示例中引用的来源,可以通过以下步骤进行:
首先检查AWS SAM模板中的资源部分。资源定义可以是Lambda函数、API Gateway、DynamoDB表等。在资源定义的属性中,可能会看到资源的ARN(Amazon Resource Name),这个ARN将成为资源引用的唯一标识符。
在资源定义之后,在AWS SAM模板中的每个函数资源的属性中,可能会看到引用其它资源的ARN。例如,Lambda函数可能会引用DynamoDB表、S3存储桶、Kinesis流等。
以下是一个AWS SAM模板示例,可以看到其中包含Lambda函数、DynamoDB表和API Gateway。Lambda函数引用了DynamoDB表,而API Gateway则配置了Lambda函数作为其后端。
Resources:
MyTable:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: my-table
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: my-function/
Handler: index.handler
Runtime: nodejs12.x
Events:
HttpEvent:
Type: Api
Properties:
Path: /my-path
Method: post
MyApi:
Type: 'AWS::Serverless::Api'
Properties:
StageName: prod
DefinitionBody:
swagger: "2.0"
info:
title: "My API"
paths:
/my-path:
post:
consumes:
- application/json
produces:
- application/json
responses:
'200':
description: 'Success'
x-amazon-apigateway-integration:
type: aws_proxy
uri:
Fn::Sub