我在 CloudFormation 模板中描述了我的 AWS 资源并面临下一个问题 - 我使用 HTTPS 协议订阅了 SNS。使用该订阅,我将 CloudWatch 警报的 SNS 主题与 API Gateway 链接,后者将这些警报转发到 Slack webhook。在我的 CloudFormation 模板中,我已经描述了 API Gateway 元素(REST API、资源和方法):
Resources:
AlarmRestApi:
Type: "AWS::ApiGateway::RestApi"
Properties:
Name: "sns-to-slack"
Description: "REST API for forwarding CloudWatch alarms to Slack"
SlackAlarmsResource:
Type: "AWS::ApiGateway::Resource"
Properties:
RestApiId: !Ref "AlarmRestApi"
ParentId: !GetAtt "AlarmRestApi.RootResourceId"
PathPart: "slack-alarms"
AlarmRestApiStage:
Type: "AWS::ApiGateway::Stage"
Properties:
DeploymentId: !Ref "RestApiDeployment"
RestApiId: !Ref "AlarmRestApi"
StageName: "dev"
...
目前我描述了 SNS 主题和订阅,但我无法获取“SlackAlarmsResource”的 PathPart 属性以避免在订阅模板中对其进行硬编码。使用硬编码的资源值,它看起来:
CloudWatchAlarmsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "cloudwatch-alarms"
TopicName: "cloudwatch-alarms"
CloudWatchAlarmsSubcscription:
Type: "AWS::SNS::Subscription"
Properties:
TopicArn: !Ref "CloudWatchAlarmsTopic"
Endpoint: !Sub "https://${AlarmRestApi}.execute-api.${AWS::Region}.amazonaws.com/${AlarmRestApiStage}/slack-alarms"
Protocol: https
RawMessageDelivery: true
如何在 SNS 订阅端点属性中获取 API Gateway 资源的 PathPart 值(“slack-alarms”)?