Skip to content

feat: examples #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/cloudformation/github-actions-odic-federation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
AWSTemplateFormatVersion: "2010-09-09"
Description: Github Actions configuration - OIDC IAM IdP Federation

Parameters:

GitHubOrganization:
Type: String
Description: This is the root organization or personal account where repos are stored (Case Sensitive)
Default: ""

RepositoryName:
Type: String
Description: The repo(s) these roles will have access to. (Use * for all org or personal repos)
Default: "*"

RoleName:
Type: String
Description: Name the Role
Default: ""


Resources:

IdpGitHubOidc:
Type: AWS::IAM::OIDCProvider
Properties:
Url: https://token.actions.githubusercontent.com
ClientIdList:
- sts.amazonaws.com
- !Sub https://github.com/${GitHubOrganization}/${RepositoryName}
ThumbprintList:
- 6938fd4d98bab03faadb97b34396831e3780aea1
Tags:
- Key: Name
Value: !Sub ${RoleName}-OIDC-Provider


Outputs:

IdpGitHubOidc:
Description: "ARN of Github OIDC Provider"
Value: !GetAtt IdpGitHubOidc.Arn
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
AWSTemplateFormatVersion: "2010-09-09"
Description: Github Actions configuration - OIDC IAM IdP and associated role CI/CD

Parameters:

GitHubOrganization:
Type: String
Description: This is the root organization or personal account where repos are stored (Case Sensitive)

RepositoryName:
Type: String
Description: The repo(s) these roles will have access to. (Use * for all org or personal repos)
Default: "*"

BranchName:
Type: String
Description: Name of the git branch to to trust. (Use * for all branches)
Default: "*"

RoleName:
Type: String
Description: Name the Role

UseExistingProvider:
Type: String
Description: "Only one GitHub Provider can exists. Choose yes if one is already present in account"
Default: "no"
AllowedValues:
- "yes"
- "no"

Conditions:

CreateProvider: !Equals ["no", !Ref UseExistingProvider]

Resources:

IdpGitHubOidc:
Type: AWS::IAM::OIDCProvider
Condition: CreateProvider
Properties:
Url: https://token.actions.githubusercontent.com
ClientIdList:
- sts.amazonaws.com
- !Sub https://github.com/${GitHubOrganization}/${RepositoryName}
ThumbprintList:
- 6938fd4d98bab03faadb97b34396831e3780aea1
Tags:
- Key: Name
Value: !Sub ${RoleName}-OIDC-Provider

RoleGithubActions:
Type: AWS::IAM::Role
Properties:
RoleName: !Ref RoleName
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Action: sts:AssumeRoleWithWebIdentity
Principal:
Federated: !If
- CreateProvider
- !Ref IdpGitHubOidc
- !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com
Condition:
StringLike:
token.actions.githubusercontent.com:sub: !Sub repo:${GitHubOrganization}/${RepositoryName}:ref:refs/heads/${BranchName}
ManagedPolicyArns:
## edit the managed policy to give least privileges
- !Sub arn:${AWS::Partition}:iam::aws:policy/AdministratorAccess

Outputs:

IdpGitHubOidc:
Condition: CreateProvider
Description: "ARN of Github OIDC Provider"
Value: !GetAtt IdpGitHubOidc.Arn

RoleGithubActionsARN:
Description: "CICD Role for GitHub Actions"
Value: !GetAtt RoleGithubActions.Arn
38 changes: 38 additions & 0 deletions examples/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: deploy
on:
push:
branches:
- main
env:
AWS_DEFAULT_REGION: us-east-1
AWS_DEFAULT_OUTPUT: json

jobs:
deploy-cfn:
name: deploy
runs-on: ubuntu-latest
# These permissions are needed to interact with GitHub’s OIDC Token endpoint.
permissions:
id-token: write
contents: read
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: us-east-1
## the following creates an ARN based on the values entered into github secrets
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_DEPLOY_ROLE }}
role-session-name: myGitHubActions
- name: Deploy EC2 Bastion
uses: aws-actions/[email protected]
with:
name: myEC2bastion
## change to path to template in your github repo
template: cloudformation/ec2-bastion.yml
capabilities: CAPABILITY_IAM, CAPABILITY_NAMED_IAM
no-fail-on-empty-changeset: "1"
## parameter set in github secrets
parameter-overrides: "pVpc=${{ secrets.VPC_ID }},pSubnet=${{ secrets.SUBNET_ID }}"