如何在使用CloudFormation部署AWS Api Gateway时获取ARN?

5
我正在尝试使用CloudFormation部署函数和AWS API Gateway。在LambdaPermission资源中,有一个属性SourceArn,它需要调用该函数的资源的ARN,此处将是API Gateway。现在ApiGateway资源没有提供ARN的输出值。那么我的问题是如何访问它?这里是Lambda Permission的资源,我需要在sourcearn中放入该值。
LambdaPermission:
    Type: "AWS::Lambda::Permission"
    Properties:
        Action: "lambda:InvokeFunction"
        FunctionName: !GetAtt LambdaFunction.Arn
        Principal: "apigateway.amazonaws.com"
        SourceArn: "How to get this value"
1个回答

6

格式:

SourceArn:
  Fn::Join:
  - ''
  - - 'arn:'
    - !Ref AWS::Partition
    - ":execute-api:"
    - !Ref AWS::Region
    - ":"
    - !Ref AWS::AccountId
    - ":"
    - !Ref "Logical ID of resource of type AWS::ApiGateway::RestApi"
    - "/"
    - !Ref "Logical ID of resource of type AWS::ApiGateway::Stage"
    - "/GET or POST or other HTTP Methods/your/resource/path/here"

一个例子:

SourceArn:
  Fn::Join:
  - ''
  - - 'arn:'
    - !Ref AWS::Partition
    - ":execute-api:"
    - !Ref AWS::Region
    - ":"
    - !Ref AWS::AccountId
    - ":"
    - !Ref ApiGatewayRestApiResource
    - "/"
    - !Ref ApiGatewayStageResource
    - "/GET/example"

感谢@Kaustubh,这个代码大约正确95%,你只需要在"ref"之前使用感叹号。这是我尝试过的并且有效。SourceArn: !Join [ ":", ["arn:aws:execute-api", !Ref AWS::Region, !Ref AWS::AccountId, !Ref ApiGatewayRestApi, "/*/POST/" ] ] - Faisal Shani
1
在 YAML 中,Ref: logicalName!Ref logicalName 都应该可以使用。然而,我编辑了我的答案,使用了 !Ref,因为这对你来说是正确的。 - Kaustubh Khavnekar
2
可以使用!Sub函数来获得更紧凑的语法。 - Henry Woody

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接