我正在尝试部署一个集成了Lambda函数的简单API网关。我正在使用GitLab CICD来部署资源,但即使尝试了这么多修复程序,我似乎也无法成功部署它。下面是我正在使用的YAML模板。
Resources:
MyFunction:
Type: AWS::Serverless::Function
FunctionName : MyFunctionName
CodeUri: MyFunctionName/
Handler: app.lambda_handler
Role: MyRole
Tags:
env : dev
Architectures:
- x86_64
Events:
MyAPI:
Type: Api
Properties:
Path: /dev
Method: POST
RestApiId:
Ref: MyAPI
VpcConfig:
SecurityGroupIds: <sec grp id>
SubnetIds: <subnet id>
MyAPI:
Type: AWS::Serverless::Api
Properties:
DefinitionBody:
swagger: 2.0
info:
title: MyAPIName
host: "xxxxx.execute-api.ap-south-1.amazonaws.com"
schemes:
- https
basePath: /dev
paths:
/:
post:
responses: {}
x-amazon-apigateway-integration:
type: "aws_proxy"
httpMethod: "POST"
uri: "arn:aws:apigateway:ap-south-1:lambda:path/2015-03-31/functions/<MyFunction ARN>/invocations"
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_match"
contentHandling: "CONVERT_TO_TEXT"
x-amazon-apigateway-policy:
Version: '2012-10-17'
Statement:
- Effect: Deny
Principal: "*"
Action: execute-api:Invoke
Resource: "*"
Condition:
StringNotEquals:
aws:sourceVpce:
- vpce-xxxxxx
- Effect: Allow
Principal: "*"
Action: execute-api:Invoke
Resource: "*"
EndpointConfiguration:
Type: PRIVATE
StageName: dev
MyAPIDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId:
Ref: MyAPI
StageName: dev
当我尝试部署上述脚本时,我收到的错误是:(使用GitLab CI(
dev already exists in stack arn:aws:cloudformation:ap-south-1:xxxx:stack/stackname/xxxx
我检查了控制台上的API GW,发现了一个名为stage的阶段(来自旧的部署(。但是没有";dev";。我认为这是问题的原因,于是删除了该阶段,并尝试再次部署。但它仍然给了我同样的错误。我已经被这个问题困扰了好几天了,任何形式的帮助都将不胜感激。
编辑
我没有正确地将这个问题联系起来——这是一种可能的方式:CloudFormation;t在更新时部署到API网关阶段(纯CFN非常复杂,但CDK提供了一些结构,可以轻松实现自定义资源,如果可能的话,我建议将代码迁移到CDK(
我在这里看到的第二种方法是创建一个lambda函数,它将等待更新完成,然后请求部署API。这个lambda应该由Cloudformation模板调用。