Chapter 17

EventBridge作成

ShigeruOda
ShigeruOda
2022.10.25に更新

■EventBridge用Role作成

img

cmd

cd ~/environment
cat << EOF > assume-role-policy-document.json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "events.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
EOF
aws iam create-role \
  --role-name ContainerHandsOnForEventBridge \
  --assume-role-policy-document file://assume-role-policy-document.json

result

{
    "Role": {
        "Path": "/",
        "RoleName": "ContainerHandsOnForEventBridge",
        "RoleId": "AROASHENIAIFOJWCZEAQ3",
        "Arn": "arn:aws:iam::123456789012:role/ContainerHandsOnForEventBridge",
        "CreateDate": "2022-09-15T12:51:00Z",
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Principal": {
                        "Service": "events.amazonaws.com"
                    },
                    "Action": "sts:AssumeRole"
                }
            ]
        }
    }
}

■EventBridge用RoleにPolicyをアタッチ

cmd

cd ~/environment
cat << EOF > InlinePolicy.json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codepipeline:StartPipelineExecution"
            ],
            "Resource": [
                "arn:aws:codepipeline:ap-northeast-1:${AccountID}:*"
            ]
        }
    ]
}
EOF
aws iam put-role-policy \
  --role-name ContainerHandsOnForEventBridge \
  --policy-name InlinePolicy \
  --policy-document file://InlinePolicy.json

result

(なし)

■EventBridgeを作成

img

cmd

aws events put-rule \
  --name "ContainerHandsOn" \
  --state "ENABLED" \
  --description "ContainerHandsOn" \
  --event-bus-name "default" \
  --event-pattern "{ \
    \"source\":[\"aws.codecommit\"], \
    \"detail-type\":[\"CodeCommit Repository State Change\"], \
    \"resources\":[\"arn:aws:codecommit:ap-northeast-1:${AccountID}:ContainerHandsOn\"], \
    \"detail\":{ \
        \"event\":[\"referenceCreated\",\"referenceUpdated\"], \
        \"referenceType\": [\"branch\"], \
        \"referenceName\":[\"master\"] \
    } \
  }" \
  --role-arn "arn:aws:iam::${AccountID}:role/ContainerHandsOnForEventBridge"

result

{
    "RuleArn": "arn:aws:events:ap-northeast-1:123456789012:rule/ContainerHandsOn"
}

■targetを作成

img

cmd

aws events put-targets \
  --rule ContainerHandsOn \
  --targets "Id"="1","Arn"="arn:aws:codepipeline:ap-northeast-1:${AccountID}:ContainerHandsOn","RoleArn"="arn:aws:iam::${AccountID}:role/ContainerHandsOnForEventBridge"

result

{
    "FailedEntryCount": 0,
    "FailedEntries": []
}