How to connect Github to AWS.

Options:

  1. Create Access Keys under the root user and add them to the CI/CD as plain text.
  2. Option 1 but use Github Secrets instead of plain text.
  3. Create an IAM user and add Access Keys and add them to the CI/CD as plain text.
  4. Option 3 but use Github Secrets instead of plain text.
  5. None of the above.


Absolutely with 100% certainty option 5. Option 4 would be a very old style way of doing it.

Note: This is not specific to Github, the same process works with Gitlab CI too and can be used in Terraform Cloud. It is also not AWS specific.


The answer to this lies with a more modern piece of technology - OIDC (Open ID Connect). More info on how this technology works under the hood here.

You'll be forgiven for not knowing Microsoft brought this into Github, as the announcements around it weren't that widely spread.

Hypothetical Greenfield situation:

You have your first AWS Account, the CTO has given you the root credentials and you want to work with CI/CD from the early stages. You have Terraform established using your own IAM keys and plan to remove these ASAP. Your Github Organisation is called "wcorp".

Create an IAM role (with an arbitrary name, I use "ci") with the access to specific services you require. Import it into Terraform. You'd then need something along these lines in your Terraform code

resource "aws_iam_openid_connect_provider" "github" {
  url = "https://token.actions.githubusercontent.com"
  client_id_list = [
    "sts.amazonaws.com",
  ]
  thumbprint_list = [
    "3ea80e902fc385f36bc08193fbc678202d572994",
    "6938fd4d98bab03faadb97b34396831e3780aea1",
  ]
}        

The actual thumbprint may change over time (not frequently) so this may need updating occasionally. AWS explain how to get the thumbprint for the provider here.

Next you need to allow Github to assume this role. Your Assume Role policy should look a little like this

data "aws_iam_policy_document" "assume_role_ci" {
  statement {
    sid = "Github"

    effect  = "Allow"
    actions = ["sts:AssumeRoleWithWebIdentity"]

    principals {
      identifiers = [aws_iam_openid_connect_provider.github.arn]
      type        = "Federated"
    }

    condition {
      test     = "StringLike"
      variable = "token.actions.githubusercontent.com:sub"
      values = [
        "repo:wcorp/*:*",
      ]
    }
  }

  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]
    principals {
      identifiers = [
        "arn:aws:iam::$AWS_Account_ID:root",
        "arn:aws:iam::$AWS_Account_ID:role/ci"
      ]
      type = "AWS"
    }
  }
}        

Obviously replace $AWS_Account_ID with your actual Account ID. Also, be careful when adding the github repo access, something like "repo:*/*:*" would allow access to anyone with a Github account.

Test the access works. Once it does, remove the manual access via Terraform you relied on earlier.

要查看或添加评论,请登录

Adam King的更多文章

  • Terraform and why you might think twice about CDK or Cloudformation.

    Terraform and why you might think twice about CDK or Cloudformation.

    Multi-Cloud Go try using Cloudformation outside of AWS, go on, in the words of every school teacher "it's your own time…

  • Setting Up Multi-Region Active-Active with AWS Global Accelerator and Aurora Global Database

    Setting Up Multi-Region Active-Active with AWS Global Accelerator and Aurora Global Database

    Ever wondered how to make your app lightning-fast for users around the world while keeping it up and running even if a…

  • What AI services do AWS provide?

    What AI services do AWS provide?

    Firstly some clarity AI is what happens, LLM (large language model) is how. AI has rapidly become more prevalent over…

  • AWS Lambda Limits

    AWS Lambda Limits

    Lambda limits can be found on the service quota page Soft limits Concurrency When invoking Lambda a container is…

  • AWS Lambda Invocations

    AWS Lambda Invocations

    There are two types of invocation Synchronous With synchronous invocation, the request to execute an AWS Lambda…

  • What should be in your AWS Security Account? (Part 2)

    What should be in your AWS Security Account? (Part 2)

    This article will be a brief intro to AWS Security Hub. What is it? AWS Security Hub is a comprehensive security…

  • What to do if you lose your EC2 Keys

    What to do if you lose your EC2 Keys

    Whilst many companies have moved onto containerised solutions, there are still a lot around relying on EC2. N.

  • The confusion of the DevOps Engineer role

    The confusion of the DevOps Engineer role

    Definition I like to summarise that as "feedback to the business and providing that in a repeated and optimal way". But…

    5 条评论
  • It's time to change.

    It's time to change.

    Let's see how much of this is familiar to the organisation that you are in or have been at in the not too distant past:…

    1 条评论
  • Thoughts on Mentoring

    Thoughts on Mentoring

    For the past few years, I've mentored those around me, with less experience; mostly through an unofficial capacity…

社区洞察

其他会员也浏览了