【发布时间】:2021-06-12 01:06:47
【问题描述】:
我有一个创建 APIGateway 的 CloudFormation 模板,但是当我再次部署以添加 APIGateway 方法(运行更新堆栈)时,我得到:
Method already exists for this resource (Service: AmazonApiGateway; Status Code: 409;
Error Code: ConflictException; Request ID: 7058deed-14e1-46bc-863d-efba23cd668b; Proxy: null)
我尝试向所有资源添加更新/删除策略,但这似乎不起作用:
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
我有一种预感,这可能是由于没有正确指定资源造成的,我的资源路径有问题吗?两个资源都是在根资源上创建的还是什么?
我的最终目标
声明两个单独的资源(具有单独的 URL)例如
https://xxx/post-note
https://xxx/deleteNote
都与 lambda 函数相关
我的模板:
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
# above two lines necessary to convert SAM template to CloudFormation template
Description: Resource definitions for my site
Globals: # any parameters you want available to all your resources
Api:
Cors:
AllowMethods: "'OPTIONS,POST,GET'"
AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'"
AllowOrigin: "'*'"
Function:
Runtime: nodejs14.x # language used at runtime
Timeout: 35 # timeout seconds for a given lambda function execution
Environment:
Variables: # these will be important later
DYNAMO_TABLE: !Ref DynamoNotesTable
DB_ENDPOINT: http://dynamodb.us-east-1.amazonaws.com
REGION_NAME: us-east-1
Resources:
# #############################################################################
# # BACK END LAMBDAS
# #############################################################################
postNote:
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Type: AWS::Serverless::Function
Properties:
Handler: backend/dynamo/postNote.postNote
CodeUri: s3://backend/xxx
Policies:
- AmazonDynamoDBFullAccess
deleteNote:
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Type: AWS::Serverless::Function
Properties:
Handler: backend/dynamo/deleteNote.deleteNote
CodeUri: s3://backend/xxx
Policies:
- AmazonDynamoDBFullAccess
#############################################################################
# api-gateway (with cognito auth)
# https://bl.ocks.org/magnetikonline/c314952045eee8e8375b82bc7ec68e88
#############################################################################
apiGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Description: API Gateway
EndpointConfiguration:
Types:
- REGIONAL
Name: my-api
postNoteGatewayMethod:
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: POST
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub
- arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
- lambdaArn: !GetAtt postNote.Arn
ResourceId: !GetAtt apiGateway.RootResourceId
RestApiId: !Ref apiGateway
deleteNoteGatewayMethod:
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: POST
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub
- arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
- lambdaArn: !GetAtt deleteNote.Arn
ResourceId: !GetAtt apiGateway.RootResourceId
RestApiId: !Ref apiGateway
apiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- postNoteGatewayMethod
- deleteNoteGatewayMethod
Properties:
RestApiId: !Ref apiGateway
StageName: Prod
lambdaApiGatewayInvoke:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt postNote.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/Prod/POST/PATH_PART
【问题讨论】:
标签: aws-lambda amazon-cloudformation aws-api-gateway