|
| 1 | +# CloudFormation template for GitHub Actions OIDC Identity Provider and IAM Role. |
| 2 | +# This template creates an IAM OIDC Identity Provider and an IAM Role that allows |
| 3 | +# GitHub Actions to securely authenticate with the PeMS AWS account. |
| 4 | + |
| 5 | +AWSTemplateFormatVersion: "2010-09-09" |
| 6 | +Description: > |
| 7 | + CloudFormation template to create an IAM OIDC Identity Provider and |
| 8 | + IAM Role for GitHub Actions |
| 9 | +
|
| 10 | +Parameters: |
| 11 | + GitHubOrgRepo: |
| 12 | + Description: Name of GitHub organization and repository (e.g., "my-org/my-repo") |
| 13 | + Type: String |
| 14 | + |
| 15 | + Thumbprint: |
| 16 | + Description: The thumbprint of the GitHub Actions OIDC provider's root CA certificate |
| 17 | + Type: String |
| 18 | + Default: d89e3bd43d5d909b47a18977aa9d5ce36cee184c |
| 19 | + |
| 20 | +Resources: |
| 21 | + GitHubOIDCProvider: |
| 22 | + Type: "AWS::IAM::OIDCProvider" |
| 23 | + DeletionPolicy: Delete |
| 24 | + Properties: |
| 25 | + ClientIdList: |
| 26 | + - "sts.amazonaws.com" |
| 27 | + ThumbprintList: |
| 28 | + - !Ref Thumbprint |
| 29 | + Url: "https://token.actions.githubusercontent.com" |
| 30 | + |
| 31 | + # This IAM Role is assumed by GitHub Actions workflows |
| 32 | + GitHubActionsRole: |
| 33 | + Type: "AWS::IAM::Role" |
| 34 | + DeletionPolicy: Delete |
| 35 | + Properties: |
| 36 | + RoleName: "pems-github-actions" |
| 37 | + Description: Assume and perform GitHub Actions in pems repo of the compilerla GitHub organization |
| 38 | + AssumeRolePolicyDocument: |
| 39 | + Version: "2012-10-17" |
| 40 | + Statement: |
| 41 | + - Effect: "Allow" |
| 42 | + Principal: |
| 43 | + # The ARN of the OIDC provider is used as the Federated principal. |
| 44 | + Federated: !Ref GitHubOIDCProvider |
| 45 | + Action: "sts:AssumeRoleWithWebIdentity" |
| 46 | + Condition: |
| 47 | + StringEquals: |
| 48 | + "token.actions.githubusercontent.com:aud": "sts.amazonaws.com" |
| 49 | + StringLike: |
| 50 | + "token.actions.githubusercontent.com:sub": !Sub "repo:${GitHubOrgRepo}:*" |
| 51 | + Policies: |
| 52 | + - PolicyName: GitHubActionsPolicy |
| 53 | + PolicyDocument: |
| 54 | + Version: "2012-10-17" |
| 55 | + Statement: |
| 56 | + - Sid: GetSSMParameterAll |
| 57 | + Effect: Allow |
| 58 | + Action: "ssm:GetParameter" |
| 59 | + Resource: "arn:aws:ssm:us-west-2:715841364638:parameter/copilot/applications/pems/*" |
| 60 | + - Sid: GetSSMParameter |
| 61 | + Effect: Allow |
| 62 | + Action: "ssm:GetParameter" |
| 63 | + Resource: "arn:aws:ssm:us-west-2:715841364638:parameter/copilot/applications/pems" |
| 64 | + - Sid: GetSSMParameterByPath |
| 65 | + Effect: Allow |
| 66 | + Action: "ssm:GetParametersByPath" |
| 67 | + Resource: "arn:aws:ssm:us-west-2:715841364638:parameter/copilot/applications/pems/components/" |
| 68 | + - Sid: AssumeRole |
| 69 | + Effect: Allow |
| 70 | + Action: "sts:AssumeRole" |
| 71 | + Resource: "arn:aws:iam::715841364638:role/pems-dev-EnvManagerRole" |
| 72 | + - Sid: ListStackInstances |
| 73 | + Effect: Allow |
| 74 | + Action: "cloudformation:ListStackInstances" |
| 75 | + Resource: "arn:aws:cloudformation:us-west-2:715841364638:stackset/pems-infrastructure:ecefc290-0f7f-470a-8b9e-b6a39adf65b7" |
| 76 | + - Sid: DescribeStacks |
| 77 | + Effect: Allow |
| 78 | + Action: "cloudformation:DescribeStacks" |
| 79 | + Resource: "arn:aws:cloudformation:us-west-2:715841364638:stack/StackSet-pems-infrastructure-c95edbbc-22a7-4239-a10b-7d73ba9344c4/45d26e70-31d1-11f0-a04a-0a0876def005" |
| 80 | + - Sid: GetAuthorizationToken |
| 81 | + Effect: Allow |
| 82 | + Action: "ecr:GetAuthorizationToken" |
| 83 | + Resource: "*" |
| 84 | + - Sid: "ECRImagePush" |
| 85 | + Effect: "Allow" |
| 86 | + Action: |
| 87 | + - "ecr:InitiateLayerUpload" |
| 88 | + - "ecr:UploadLayerPart" |
| 89 | + - "ecr:CompleteLayerUpload" |
| 90 | + - "ecr:PutImage" |
| 91 | + - "ecr:BatchCheckLayerAvailability" |
| 92 | + - "ecr:GetDownloadUrlForLayer" |
| 93 | + - "ecr:GetRepositoryPolicy" |
| 94 | + - "ecr:DescribeRepositories" |
| 95 | + - "ecr:ListImages" |
| 96 | + - "ecr:DescribeImages" |
| 97 | + - "ecr:BatchGetImage" |
| 98 | + - "ecr:GetLifecyclePolicy" |
| 99 | + - "ecr:GetLifecyclePolicyPreview" |
| 100 | + - "ecr:ListTagsForResource" |
| 101 | + - "ecr:DescribeImageScanFindings" |
| 102 | + Resource: "arn:aws:ecr:us-west-2:715841364638:repository/pems/*" |
| 103 | + |
| 104 | +Outputs: |
| 105 | + OIDCProviderArn: |
| 106 | + Description: The ARN of the created OIDC Provider |
| 107 | + Value: !Ref GitHubOIDCProvider |
| 108 | + GitHubActionsRoleArn: |
| 109 | + Description: The ARN of the IAM Role for GitHub Actions |
| 110 | + Value: !GetAtt GitHubActionsRole.Arn |
0 commit comments