ARN 作为 Cloudformation 堆栈中的参数

ARN as a parameter in Cloud Formation Stack

我想使用 ARN 作为 cloudformation 堆栈资源 EventRuleRegion1 - Target 以及 EventBridgeIAMrole 的参数输入,但它不起作用。当我使用 Ref 函数调用时

原始 ARN

arn:aws:events:ap-southeast-2:123456789123:event-bus/central-eventbus-sydney

当我直接在代码中提供 arn 时,它工作正常。

代码

AWSTemplateFormatVersion: 2010-09-09

Parameters:
  EventBridgeName:
    Description: Enter the Event Bridge Name
    Type: String
    Default: ec2-lifecycle-events
    
  EventBusName:
    Description: Enter the Central Event Bus Name
    Type: String
    Default: central-eventbus-sydney
    
  EventBusArn:
    Description: Enter the ARN of Central Event Bus
    Type: String
    Default: arn:aws:events:ap-southeast-2:123456789123:event-bus/central-eventbus-sydney
    
  Monitoringaccount:
    Description: Enter the Monitoring AWS account number
    Type: String
    Default: 123456789123

Resources:
    EventRuleRegion1:
        Type: AWS::Events::Rule
        Properties: 
            Description: Event rule to send events to monitoring account event bus
            EventBusName: default
            EventPattern:
                source:
                    - aws.ec2
                detail-type:
                    - "EC2 Instance State-change Notification"
                detail:
                  state:
                    - "running"
                    - "stopped"
                    - "terminated"
                    
            Name: !Ref EventBridgeName
            State: ENABLED
            Targets: 
                - Arn: >-
                    - !Join [ "", [ !Sub "arn:aws:events:${AWS::Region}:123456789123:event-bus/",!Ref EventBusName ] ]
                  Id: !Ref EventBusName
                  RoleArn: !GetAtt
                    - EventBridgeIAMrole
                    - Arn      
                  
    
    
    EventBridgeIAMrole:
        Type: 'AWS::IAM::Role'
        Properties:
            AssumeRolePolicyDocument:
                Version: 2012-10-17
                Statement:
                    - Effect: Allow
                      Principal:
                        Service: !Sub events.amazonaws.com
                      Action: 'sts:AssumeRole'
            Path: /
            Policies:
                - PolicyName: PutEventsDestinationBus
                  PolicyDocument:
                    Version: 2012-10-17
                    Statement:
                        - Effect: Allow
                          Action:
                            - 'events:PutEvents'
                          Resource:
                            - >-
                              - !Join [ "", [ !Sub "arn:aws:events:${AWS::Region}:123456789123:event-bus/",!Ref EventBusName ] ]

错误

Parameter - !Join [ "", [ !Sub "arn:aws:events:${AWS::Region}:123456789123:event-bus/",!Ref EventBusName ] ] is not valid. Reason: Provided Arn is not in correct format. (Service: AmazonCloudWatchEvents; Status Code: 400; Error Code: ValidationException; Request ID: 0d52a1d6-095e-44f7-9455-b7481dc4fb8d; Proxy: null)

>- 的使用将导致 文字字符串 ,而不是计算您的 CFN 函数(join、ref)。应该是:

            Targets: 
                - Arn: !Join [ "", [ !Sub "arn:aws:events:${AWS::Region}:123456789123:event-bus/",!Ref EventBusName ] ]