如果您在使用AWS CloudFormation创建API Gateway时遇到“Api事件必须引用同一模板中的Api”错误,则需要进行以下步骤来解决该问题。
Resources:
ApiGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Name: MyApiGateway
MyFunction:
Type: 'AWS::Lambda::Function'
Properties:
Code:
S3Bucket: myBucket
S3Key: myKey
Handler: index.handler
Role: !GetAtt [ MyFunctionRole, Arn ]
Runtime: nodejs12.x
MyFunctionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: MyFunctionPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action:
- s3:GetObject
Resource: arn:aws:s3:::myBucket/*
MyApiGatewayMethod:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: POST