将lambda附加到现有API GW cloudformation



我试图将cloudformation中的lambda函数附加到其他模板中创建的现有API网关。

在另一个模板中导出了一些变量:

CreatedApiGW和CreatedApiGWParentId

FunctionTest:
Type: AWS::Serverless::Function
Properties:
FunctionName: function-test
CodeUri: cmd/
VpcConfig:
SecurityGroupIds:
- !Ref SecGroup
SubnetIds:
- !Ref Subnet1
- !Ref Subnet2

ApiGatewayResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !ImportValue CreatedApiGW
ParentId: !ImportValue CreatedApiGWParentId
PathPart: "test"
ApiGatewayMethod:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !ImportValue CreatedApiGW
ResourceId: !Ref ApiGatewayResource
HttpMethod: "POST"
AuthorizationType: "NONE"
Integration:
Type: "AWS"
IntegrationHttpMethod: "POST"
Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${FunctionTest.Arn}/invocations"

ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !ImportValue CreatedApiGW

我不想为每个lambda创建一个API Gateway。

我想附加一些lambdas到我现有的API网关。

我的lambda没有创建API GW的触发器

将lambda附加到API GW

好吧,我假设您已经创建了Api,并希望导入在上述模板中导出的值。我还假设您创建了一个RestApi(在任何情况下,两个文档引用都将在末尾)。

您需要导出另一个堆栈中的值,例如:

Outputs:
ApiGatewayId:
Description: The ID of the api you just created
Value:
Ref: ApiGateway
Export:
Name:
'Fn::Sub': '${AWS::StackName}-CreatedApiGW'
ApiGatewayRootResourceId:
Description: The security group ID to use for public web servers
Value:
'Fn::GetAtt':
- ApiGateway
- RootResourceId
Export:
Name:
'Fn::Sub': '${AWS::StackName}-RootResourceId'

导入将看起来像这样:

Fn::ImportValue:
!Sub "${ApiGwStackName}-CreatedApiGW"

Fn::ImportValue:
!Sub "${ApiGwStackName}-RootResourceId"

代码的其余部分看起来很好。您可以在模板中为任何apiggateway创建方法,您只需要apigw id和根资源id。

一切创建完成后,触发器将出现在函数页中。

参考:

  • Fn: ImportValue
  • AWS:: apiggateway::RestApi返回值

最新更新