Thuta Learning
AWS Advanced
IaC & DeliveryDevOpsအဆင့်မြင့်

OIDC နဲ့ CI/CD — Access key မပါဘဲ GitHub Actions ကနေ AWS ကို deploy လုပ်ခြင်း

AWS Advancedသင်ခန်းစာ 1250 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • CI ထဲက long-lived access key ဘာကြောင့် အန္တရာယ်ရှိလဲ ရှင်းပြနိုင်မယ်
  • GitHub Actions OIDC token ကနေ temporary credential ရတဲ့ flow တစ်ခုလုံးကို ရှင်းပြနိုင်မယ်
  • aud နဲ့ sub condition နဲ့ repo + branch/environment ကို pin လုပ်ထားတဲ့ role trust policy ရေးနိုင်မယ်
  • id-token: write နဲ့ configure-aws-credentials သုံးပြီး CDK ကို deploy လုပ်တဲ့ workflow ရေးနိုင်မယ်
  • Cross-account deploy role, approval gate နဲ့ CodePipeline/CodeBuild alternative ကို ဘယ်အချိန် သုံးမလဲ ဆုံးဖြတ်နိုင်မယ်

ရိုးရိုးလေး ရှင်းပြမယ်

OIDC ကို ရိုးရိုးလေး

Access key ကို ရုံးခန်းရဲ့ သော့အစစ်လို့ မြင်ကြည့်ပါ။ CI system ကို သော့တစ်ချောင်း ပွားပေးလိုက်ရင် အဲဒီသော့က ဘယ်တော့မှ သက်တမ်းမကုန်ဘဲ copy ကူးတဲ့သူတိုင်း အချိန်မရွေး ဝင်လို့ ရပါတယ်။

OIDC ကတော့ ဧည့်သည် badge စနစ်လိုပါပဲ။ Workflow run တိုင်းမှာ GitHub က 'ဒါ acme/orders-service repo ရဲ့ main branch ကနေ run နေတဲ့ job ပါ' လို့ ရေးထားတဲ့ လက်မှတ်ထိုး ID ကတ်ပြားလေး (token) တစ်ခု ထုတ်ပေးပါတယ်။ AWS က ကတ်ပြားကို စစ်ပြီး ကိုက်ညီရင် တစ်နာရီလောက်ပဲ သက်တမ်းရှိတဲ့ ယာယီ badge (temporary credential) ကို ပေးပါတယ်။ သိမ်းထားစရာ သော့ မရှိတော့လို့ ခိုးစရာလည်း မရှိတော့ပါဘူး။

Problem → Why → Solution

Repository secret ထဲက access key

CI ကနေ AWS ဆီ deploy လုပ်ဖို့ အလွယ်ဆုံးနည်းက IAM user တစ်ယောက် ဆောက်၊ access key ထုတ်၊ AWS_ACCESS_KEY_ID နဲ့ AWS_SECRET_ACCESS_KEY ကို repository secret ထဲ ထည့်တာပါ။ အလုပ်တော့ လုပ်ပါတယ် — ဒါပေမယ့် key က သက်တမ်းမကုန်ဘဲ rotate လုပ်ဖို့ လူက မှတ်ထားရပါတယ်။ Deploy လုပ်ဖို့ ဖြစ်လို့ permission ကလည်း ကျယ်ပြန့်တတ်ပါတယ်။

Key ယိုစိမ့်နိုင်တဲ့ လမ်းတွေ အများကြီးပါ — workflow log ထဲ debug နဲ့ print မိတာ၊ compromised ဖြစ်နေတဲ့ third-party action တစ်ခုက environment variable တွေကို ပို့လိုက်တာ၊ repo ကို fork/copy လုပ်ရင်း ပါသွားတာ၊ ဝန်ထမ်းဟောင်း laptop ထဲ ကျန်နေတာ စသဖြင့်။ ယိုစိမ့်သွားတာနဲ့ revoke မလုပ်မချင်း ကမ္ဘာ့ ဘယ်နေရာကမဆို သုံးလို့ ရနေပါတယ်။

.github/workflows/deploy-old.yml
# The old way: a long-lived IAM user key stored as repository secrets
name: deploy-old
on:
  push:
    branches: [main]
jobs:
  deploy:
    runs-on: ubuntu-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}         # never expires
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # exposed to every step
      AWS_DEFAULT_REGION: us-east-1
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npx cdk deploy --all --require-approval never

ဘာကြောင့် ဒါက အန္တရာယ်လဲ

Long-lived credential က 'ဘယ်သူလဲ' ကို သက်သေပြတာ မဟုတ်ဘဲ 'ဘာကို ကိုင်ထားလဲ' ကိုပဲ စစ်ပါတယ်။ Key ကိုင်ထားသူ မှန်သမျှ — workflow ဖြစ်ဖြစ်၊ attacker ဖြစ်ဖြစ် — တူတူပါပဲ။ OIDC မှာတော့ credential တိုင်းကို run တစ်ခုချင်းအတွက် ထုတ်ပြီး repo, branch, environment ကို AWS က trust policy နဲ့ စစ်ပါတယ်၊ credential ကလည်း မိနစ်အနည်းငယ်ကနေ နာရီအနည်းငယ်အတွင်း သက်တမ်းကုန်ပါတယ်။

Token ကနေ temporary credential အထိ

GitHub Actions ဟာ OpenID Connect (OIDC) identity provider တစ်ခု ဖြစ်ပါတယ်။ Job တစ်ခုမှာ id-token: write permission ပါရင် runner က https://token.actions.githubusercontent.com ဆီကနေ JSON Web Token (JWT) တစ်ခု တောင်းလို့ ရပါတယ်။ Token ကို GitHub က sign လုပ်ထားပြီး အထဲမှာ claim တွေ ပါပါတယ် — iss (ထုတ်ပေးသူ)၊ aud (ဘယ်သူ့အတွက်လဲ — AWS အတွက်ဆို sts.amazonaws.com)၊ sub (ဘယ် repo, ဘယ် branch/environment/event)၊ နဲ့ repository, ref, run_id လို အသေးစိတ်တွေပါ။

sub claim
Token ရဲ့ subject ပါ — ဒီ run ဘယ်ကလာလဲ ဆိုတာကို string တစ်ခုအဖြစ် ဖော်ပြတာပါ။ ဥပမာ repo:acme/orders-service:ref:refs/heads/main (branch push)၊ repo:acme/orders-service:environment:production (environment ကို သုံးတဲ့ job)၊ repo:acme/orders-service:pull_request (PR)။ Trust policy ရဲ့ လုံခြုံရေး အများစုဟာ ဒီ string ကို တင်းကျပ်စွာ စစ်တာပေါ်မှာ မူတည်ပါတယ်။

AWS ဘက်မှာ account တစ်ခုချင်းကို တစ်ကြိမ် IAM OIDC identity provider တစ်ခု ဖန်တီးပါတယ် — 'token.actions.githubusercontent.com က sign လုပ်တဲ့ token တွေကို ယုံတယ်' လို့ ပြောတာပါ။ ပြီးရင် IAM role တစ်ခု ဆောက်ပြီး trust policy မှာ အဲဒီ provider ကို Federated principal အဖြစ်၊ sts:AssumeRoleWithWebIdentity ကို action အဖြစ် ထည့်ပါတယ်။ Condition နှစ်ခုက အရေးကြီးဆုံးပါ — token.actions.githubusercontent.com:aud က sts.amazonaws.com ဖြစ်ရမယ်၊ token.actions.githubusercontent.com:sub က ကိုယ့် repo နဲ့ branch (သို့) environment အတိအကျ ဖြစ်ရမယ်။

Workflow ထဲမှာ aws-actions/configure-aws-credentials action က token ကို တောင်းပြီး STS ရဲ့ AssumeRoleWithWebIdentity ကို ခေါ်ပါတယ်။ STS က token ရဲ့ signature, သက်တမ်းနဲ့ condition တွေကို စစ်ပြီး temporary access key, secret, session token ကို ပြန်ပေးပါတယ်။ ဒါတွေကို နောက် step တွေအတွက် environment variable အဖြစ် ထည့်ပေးလို့ aws CLI နဲ့ cdk deploy က ပုံမှန်အတိုင်း အလုပ်လုပ်ပါတယ်။ ဘာ secret မှ repository ထဲ သိမ်းထားစရာ မလိုပါဘူး။

ဘာတွေ ပျောက်သွားလဲ သတိထားကြည့်ပါ — IAM user မရှိတော့ပါဘူး၊ GitHub ထဲမှာ secret လည်း မရှိတော့ပါဘူး။ JWT ကိုယ်တိုင်က မိနစ်အနည်းငယ်အတွင်း သက်တမ်းကုန်ပြီး သူ့ audience အတွက်ပဲ အသုံးဝင်ပါတယ်။ AWS credential တွေကလည်း default အားဖြင့် တစ်နာရီပဲ ခံပြီး role ရဲ့ maximum session duration ထက် မကျော်နိုင်ပါဘူး။ ယိုစိမ့်သွားတဲ့ log line တစ်ကြောင်းဟာ တစ်နာရီကြာရင် attacker အတွက် တန်ဖိုး မရှိသလောက် ဖြစ်သွားပါပြီ။

text
GITHUB ACTIONS OIDC TO AWS
--------------------------
 GitHub Actions runner                         AWS account 111122223333
 +------------------------+  1. request JWT   +---------------------------+
 | job (id-token: write)  | ----------------> | GitHub OIDC issuer        |
 |                        | <---------------- | token.actions.github...   |
 |                        |  2. signed token  +---------------------------+
 |                        |     (sub = repo + branch or environment)
 |                        |
 |                        |  3. AssumeRoleWithWebIdentity(token, role ARN)
 |                        | ----------------> +---------------------------+
 |                        |                   | AWS STS                   |
 |                        |                   |  - signature via IAM OIDC |
 |                        |                   |    identity provider      |
 |                        |                   |  - trust policy aud + sub |
 |                        | <---------------- +---------------------------+
 | aws / cdk deploy       |  4. temporary credentials (about 1 hour)
 +------------------------+
ကတ်ပြား → စစ် → badge
  1. Job asks GitHub for a signed JWT
  2. Action calls AssumeRoleWithWebIdentity
  3. STS checks aud and sub conditions
  4. Short-lived credentials for this run

Trust policy ရဲ့ sub condition ဟာ တကယ့် လုံခြုံရေး တံခါးပါ။ sub ကို မစစ်ရင် GitHub ပေါ်က ဘယ် repo ကမဆို ကိုယ့် role ကို assume လုပ်ဖို့ ကြိုးစားလို့ ရပါတယ်။

Repo + environment ကို pin လုပ်ထားတဲ့ trust policy

trust-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "GitHubActionsProductionDeploy",
      "Effect": "Allow",
      "Principal": {
        "Federated":
          "arn:aws:iam::111122223333:oidc-provider/token.actions.githubusercontent.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
          "token.actions.githubusercontent.com:sub":
            "repo:acme/orders-service:environment:production"
        }
      }
    }
  ]
}

Principal က account ထဲက GitHub OIDC provider ဖြစ်ပြီး action က sts:AssumeRoleWithWebIdentity ပါ။ StringEquals နဲ့ aud ကို sts.amazonaws.com၊ sub ကို acme/orders-service repo ရဲ့ production environment ကို အတိအကျ ချည်ထားပါတယ်။ Branch push တစ်ခုတည်းကို ခွင့်ပြုချင်ရင် sub ကို repo:acme/orders-service:ref:refs/heads/main လို့ ထားပါ။

Test ပြီးမှ approval နဲ့ CDK deploy လုပ်တဲ့ workflow

ဒီ workflow မှာ job နှစ်ခု ရှိပါတယ်။ test job က AWS credential မလိုဘဲ unit test နဲ့ cdk synth ကို run ပါတယ်။ deploy job က production GitHub environment ကို သုံးလို့ environment ရဲ့ required reviewer က approve မလုပ်မချင်း စောင့်ပါတယ် — ဒါက approval gate ပါ။ Approve ပြီးမှ OIDC နဲ့ role ကို assume လုပ်ပြီး account 111122223333 ထဲက OrdersStack လို CDK app ကို deploy လုပ်ပါတယ်။

id-token ကို job အဆင့်မှာပဲ ပေးပါ

Workflow အဆင့်မှာ contents: read ကိုပဲ ပေးပြီး id-token: write ကို deploy job ထဲမှာပဲ ထည့်ထားပါတယ်။ Test job ထဲက dependency တစ်ခု compromise ဖြစ်ခဲ့ရင်တောင် AWS token ကို တောင်းလို့ မရပါဘူး။

.github/workflows/deploy.yml
name: deploy-orders
on:
  push:
    branches: [main]

permissions:
  contents: read          # default for every job: read the repo, nothing else

concurrency:
  group: deploy-production
  cancel-in-progress: false

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm test
      - run: npx cdk synth --quiet

  deploy:
    needs: test
    runs-on: ubuntu-latest
    environment: production   # required reviewers = approval gate
    permissions:
      contents: read
      id-token: write         # lets this job request the OIDC token
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - name: Assume the deploy role with OIDC (no stored keys)
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::111122223333:role/github-orders-deploy
          role-session-name: gha-${{ github.run_id }}
          aws-region: us-east-1
      - run: aws sts get-caller-identity
      - run: npx cdk deploy --all --require-approval never

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. permissionspermissions: contents: read

    Workflow တစ်ခုလုံးရဲ့ default permission ကို အနည်းဆုံး ထားပါတယ်။ permissions block တစ်ခု ရေးလိုက်တာနဲ့ မဖော်ပြတဲ့ permission အားလုံး none ဖြစ်သွားပါတယ်။

  2. concurrencygroup: deploy-production

    Production deploy နှစ်ခု တစ်ပြိုင်နက် မ run အောင် တားပါတယ်။ cancel-in-progress: false က run နေဆဲ CloudFormation update ကို အလယ်မှာ မဖြတ်အောင်ပါ။

  3. environment: productionenvironment: production

    GitHub environment ရဲ့ protection rule (required reviewer, wait timer, ခွင့်ပြုထားတဲ့ branch) တွေကို job ပေါ် အသုံးချပါတယ်။ ဒါ့အပြင် token ရဲ့ sub ကို repo:acme/orders-service:environment:production ဖြစ်စေလို့ syntax section ရဲ့ trust policy နဲ့ ကိုက်ပါတယ်။

  4. id-token: writeid-token: write

    ဒီ permission မပါရင် runner က OIDC token မတောင်းနိုင်လို့ configure-aws-credentials step ဟာ credential မရဘဲ fail ဖြစ်ပါတယ်။ Repository ကို ရေးခွင့် မပေးပါဘူး — token တောင်းခွင့်သာ ဖြစ်ပါတယ်။

  5. configure-aws-credentialsrole-to-assume / role-session-name / aws-region

    Token ကို တောင်း၊ AssumeRoleWithWebIdentity ကို ခေါ်၊ temporary credential ကို နောက် step တွေအတွက် export လုပ်ပေးပါတယ်။ role-session-name ထဲ run_id ထည့်ထားလို့ CloudTrail မှာ API call တိုင်းကို ဘယ် workflow run က လုပ်လဲ ခြေရာခံလို့ ရပါတယ်။

  6. cdk deploynpx cdk deploy --all --require-approval never

    CI ထဲမှာ interactive prompt မရှိလို့ --require-approval never ထားပါတယ်။ လူ့ approval ကတော့ GitHub environment မှာ ရှိပြီးသားပါ။ CDK CLI က ဒီ role နဲ့ bootstrap role တွေကို assume လုပ်ပြီး deploy လုပ်ပါတယ်။

You should see
Sample output from the deploy job (IDs will differ):

Run aws sts get-caller-identity
{
    "UserId": "AROA3EXAMPLEROLEID:gha-11223344556",
    "Account": "111122223333",
    "Arn": "arn:aws:sts::111122223333:assumed-role/github-orders-deploy/gha-11223344556"
}
Run npx cdk deploy --all --require-approval never
OrdersStack: deploying... [1/1]
OrdersStack: creating CloudFormation changeset...
OrdersStack (no changes)
Push ကနေ production အထိ
  1. Push to main
  2. test job: unit tests + cdk synth
  3. deploy job waits for reviewer
  4. OIDC -> AssumeRoleWithWebIdentity
  5. cdk deploy via CloudFormation

Reviewer က reject

Job never gets an AWS token

sub မကိုက်

STS: Not authorized to perform AssumeRoleWithWebIdentity

အားလုံး ကိုက်

Stack updated, credentials expire

AWS ဘက်ကို တည်ဆောက်ခြင်း — CLI နဲ့ CDK

Account တစ်ခုမှာ token.actions.githubusercontent.com အတွက် OIDC provider တစ်ခုပဲ ရှိနိုင်ပါတယ်၊ role တွေကတော့ repo/environment အလိုက် အများကြီး ဆောက်လို့ရပါတယ်။ AWS က GitHub ရဲ့ certificate ကို ကိုယ်တိုင် စစ်ဆေးလို့ thumbprint ကို ကိုယ်တိုင် ထည့်ပေးစရာ မလိုတော့ပါဘူး။

Role ရဲ့ permission အတွက် အရေးကြီးတဲ့ အချက်တစ်ခုက CDK deploy လုပ်ဖို့ CI role ကို AdministratorAccess ပေးစရာ မလိုတာပါ။ CDK CLI က bootstrap လုပ်တုန်းက ဆောက်ထားတဲ့ cdk-hnb659fds-* role တွေ (deploy, file-publishing, image-publishing, lookup) ကို assume လုပ်ပြီး တကယ့် resource တွေကိုတော့ CloudFormation execution role က ဆောက်ပါတယ်။ ဒါကြောင့် CI role မှာ အဲဒီ role တွေကို sts:AssumeRole လုပ်ခွင့်ပဲ လိုပါတယ်။

setup-oidc.sh
# 1. Trust GitHub's OIDC issuer (once per account)
aws iam create-open-id-connect-provider \
  --url https://token.actions.githubusercontent.com \
  --client-id-list sts.amazonaws.com

# 2. The deploy role, trusted only by repo + environment (trust-policy.json above)
aws iam create-role --role-name github-orders-deploy \
  --assume-role-policy-document file://trust-policy.json \
  --max-session-duration 3600

# 3. Its only permission: assume the CDK bootstrap roles
aws iam put-role-policy --role-name github-orders-deploy \
  --policy-name assume-cdk-bootstrap-roles \
  --policy-document file://assume-cdk-roles.json
assume-cdk-roles.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AssumeCdkBootstrapRoles",
      "Effect": "Allow",
      "Action": "sts:AssumeRole",
      "Resource": "arn:aws:iam::111122223333:role/cdk-hnb659fds-*"
    }
  ]
}

ဒီ setup ကိုယ်တိုင်ကိုလည်း infrastructure as code နဲ့ ထိန်းသင့်ပါတယ်။ အောက်က CDK app က provider (native AWS::IAM::OIDCProvider resource ကို သုံးတဲ့ OidcProviderNative)၊ trust condition ပါတဲ့ role နဲ့ bootstrap role တွေကို assume လုပ်ခွင့်ကို ဆောက်ပါတယ်။ ဒီ stack ကိုတော့ pipeline မရှိသေးခင် admin တစ်ယောက်က တစ်ကြိမ် deploy လုပ်ရပါမယ် — ကြက်နဲ့ ဥ ပြဿနာပါ။

bin/github-access.ts
import { App, CfnOutput, Duration, Stack, StackProps } from "aws-cdk-lib";
import * as iam from "aws-cdk-lib/aws-iam";
import { Construct } from "constructs";

interface GitHubDeployAccessProps extends StackProps {
  repo: string;         // "owner/name"
  environment: string;  // GitHub environment allowed to deploy
}

export class GitHubDeployAccessStack extends Stack {
  constructor(scope: Construct, id: string, props: GitHubDeployAccessProps) {
    super(scope, id, props);

    const provider = new iam.OidcProviderNative(this, "GitHubOidc", {
      url: "https://token.actions.githubusercontent.com",
      clientIds: ["sts.amazonaws.com"],
    });

    const role = new iam.Role(this, "DeployRole", {
      roleName: "github-orders-deploy",
      maxSessionDuration: Duration.hours(1),
      assumedBy: new iam.OpenIdConnectPrincipal(provider, {
        StringEquals: {
          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
          "token.actions.githubusercontent.com:sub":
            `repo:${props.repo}:environment:${props.environment}`,
        },
      }),
    });

    // CloudFormation does the real work through the bootstrap execution role,
    // so CI only needs to assume the CDK bootstrap roles.
    role.addToPolicy(new iam.PolicyStatement({
      actions: ["sts:AssumeRole"],
      resources: [`arn:aws:iam::${this.account}:role/cdk-hnb659fds-*`],
    }));

    new CfnOutput(this, "DeployRoleArn", { value: role.roleArn });
  }
}

const app = new App();
new GitHubDeployAccessStack(app, "GitHubDeployAccess", {
  env: { account: "111122223333", region: "us-east-1" },
  repo: "acme/orders-service",
  environment: "production",
});
app.synth();

Cross-account deploy

Production ကို dev နဲ့ account ခွဲထားတာ (Architecture & Governance chapter က multi-account strategy) ပုံမှန်ပါ။ Pattern နှစ်မျိုး ရှိပါတယ်။ ပထမ pattern က target account တိုင်းမှာ ကိုယ်ပိုင် OIDC provider နဲ့ role ရှိပြီး dev account ရဲ့ role က environment:dev ကို၊ prod account ရဲ့ role က environment:production ကို trust လုပ်တာပါ။ ရိုးရှင်းပြီး blast radius သေးပါတယ်။

ဒုတိယ pattern က tooling (CI) account တစ်ခုထဲမှာပဲ OIDC role ထားပြီး target account တွေကို cdk bootstrap --trust နဲ့ bootstrap လုပ်တာပါ။ --trust က target account ရဲ့ bootstrap role တွေကို tooling account က assume လုပ်ခွင့်ပေးပြီး --cloudformation-execution-policies က CloudFormation ရဲ့ permission ကို ကန့်သတ်ပါတယ်။ ဒါဆို CI role ရဲ့ policy မှာ arn:aws:iam::444455556666:role/cdk-hnb659fds-* လို target account role ARN တွေကို ထည့်ပေးရပါမယ်။

bootstrap-prod.sh
# Run once in the PROD account (444455556666) with its admin credentials.
# Trust the tooling account and cap what CloudFormation may create there.
npx cdk bootstrap aws://444455556666/us-east-1 \
  --trust 111122223333 \
  --cloudformation-execution-policies \
  arn:aws:iam::444455556666:policy/orders-cfn-execution

# Confirm the bootstrap version and the roles it created
aws ssm get-parameter --name /cdk-bootstrap/hnb659fds/version \
  --query Parameter.Value --output text
aws iam list-roles --query "Roles[?starts_with(RoleName, 'cdk-hnb659fds')].RoleName"
Account တိုင်းမှာ OIDC roleTooling account + --trust
Setupရိုးရှင်း၊ account တစ်ခုချင်း သီးခြားBootstrap trust ကို စီမံရ
Blast radiusRole တစ်ခု = account တစ်ခုCI role က account အများကြီးကို ရောက်
သင့်တော်တဲ့နေရာGitHub Actions + environment တွေCDK Pipelines, CodePipeline, account အများကြီး

AWS-native alternative — CodePipeline နဲ့ CodeBuild

CI/CD တစ်ခုလုံးကို AWS ထဲမှာပဲ ထားချင်ရင် CodePipeline (stage တွေ ချိတ်ပေးတဲ့ orchestrator) နဲ့ CodeBuild (build container တွေ run တဲ့ service) ကို သုံးနိုင်ပါတယ်။ GitHub ကို CodeConnections (ယခင် CodeStar Connections) နဲ့ ချိတ်ပါတယ်။ ဒီမှာ OIDC မလိုပါဘူး — CodeBuild project မှာ service role တစ်ခု ရှိပြီး build container ထဲကို temporary credential အလိုအလျောက် ရောက်လာပါတယ်။ Approval gate ကို CodePipeline ရဲ့ Manual approval action နဲ့ လုပ်ပြီး SNS နဲ့ reviewer ကို အကြောင်းကြားပါတယ်။

CDK project တွေအတွက် aws-cdk-lib/pipelines module (CDK Pipelines) က CodePipeline ကို ကိုယ်တိုင် update လုပ်တဲ့ (self-mutating) pipeline အဖြစ် ဆောက်ပေးပြီး stage အသစ် ထည့်တာကို code တစ်ကြောင်းနဲ့ လုပ်လို့ ရပါတယ်။

buildspec.yml
version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 22
    commands:
      - npm ci
  build:
    commands:
      - npm test
      - npx cdk synth --quiet
artifacts:
  base-directory: cdk.out
  files:
    - "**/*"
GitHub Actions + OIDCCodePipeline + CodeBuild
AWS credentialOIDC token → roleCodeBuild service role (အလိုအလျောက်)
Approval gateEnvironment required reviewerManual approval action
Build ပတ်ဝန်းကျင်GitHub-hosted (သို့) self-hosted runnerVPC ထဲမှာပါ run လို့ရတဲ့ CodeBuild container
ဘယ်အချိန်Code နဲ့ PR တွေ GitHub မှာ ရှိပြီး developer တွေ ရင်းနှီးတယ်Private network access, AWS-only audit, CDK Pipelines လိုချင်

Production မှာ

Production pipeline တစ်ခု ဘယ်လို ပုံစံလဲ

ပုံမှန် setup တစ်ခုမှာ GitHub environment သုံးခု (dev, staging, production) ရှိပြီး environment တစ်ခုချင်းက AWS account တစ်ခုနဲ့ ကိုက်ပါတယ်။ Account တစ်ခုချင်းမှာ role တစ်ခုစီ ရှိပြီး trust policy က သက်ဆိုင်ရာ environment ကိုပဲ ခွင့်ပြုပါတယ်။ Production environment မှာ required reviewer နှစ်ယောက်၊ main branch ကပဲ deploy လုပ်ခွင့် (deployment branch rule) ထားပါတယ်။ Pull request တိုင်းမှာ read-only role နဲ့ cdk diff run ပြီး ရလဒ်ကို PR comment အဖြစ် တင်ပါတယ်။

Migration လုပ်တဲ့အခါ OIDC role အလုပ်လုပ်ပြီဆိုတာ သေချာတာနဲ့ IAM user ရဲ့ access key ကို deactivate လုပ်၊ CloudTrail မှာ အသုံးပြုမှု မရှိတော့ကြောင်း တစ်ပတ်လောက် စောင့်ကြည့်ပြီးမှ user ကိုပါ ဖျက်ပါ။ IAM Access Analyzer နဲ့ ကိုယ့် role တွေရဲ့ trust policy ကို စစ်ပြီး sub condition မပါတဲ့ role တွေကို SCP (သို့) automated check နဲ့ တားတဲ့ organization တွေလည်း များလာပါတယ်။ CloudTrail ရဲ့ AssumeRoleWithWebIdentity event တွေမှာ role-session-name ကနေ workflow run ကို ပြန်ခြေရာခံလို့ ရပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားများ

  • sub condition ကို ချန်ထားခြင်း ဒါမှမဟုတ် ကျယ်လွန်းစွာ wildcard သုံးခြင်း — aud ကိုပဲ စစ်ရင် GitHub ပေါ်က ဘယ် repo ရဲ့ workflow ကမဆို ကိုယ့် role ကို assume လုပ်ဖို့ ကြိုးစားလို့ရပါတယ်။ StringLike နဲ့ repo:acme/* ဆိုရင်လည်း organization ထဲက repo တိုင်း (intern ရဲ့ စမ်းသပ် repo အပါအဝင်) production ကို deploy လုပ်နိုင်သွားပါတယ်။
  • Environment နဲ့ branch sub ကို ရောထွေးခြင်း — job မှာ environment: production ပါရင် sub က ...:environment:production ဖြစ်သွားပြီး ...:ref:refs/heads/main မဟုတ်တော့ပါဘူး။ Trust policy က branch form ကို စစ်နေရင် 'Not authorized to perform sts:AssumeRoleWithWebIdentity' ရပါတယ်။
  • id-token: write ကို မေ့ခြင်း — configure-aws-credentials က credential မရဘဲ fail ဖြစ်ပါတယ်။ permissions block ကို workflow အဆင့်မှာ ရေးထားပြီး job အဆင့်မှာ ထပ်ရေးရင် job အဆင့်က အစားထိုးတာကိုလည်း သတိပြုပါ။
  • CI role ကို AdministratorAccess ပေးခြင်း — CDK အတွက် bootstrap role တွေကို assume လုပ်ခွင့်ပဲ လိုပါတယ်။ Admin ပေးလိုက်ရင် workflow ထဲက compromised step တစ်ခုက account တစ်ခုလုံးကို ထိန်းချုပ်နိုင်ပါတယ်။
  • pull_request_target trigger နဲ့ fork က code ကို run ပြီး AWS role assume လုပ်ခြင်း — အပြင်လူရဲ့ code ကို ကိုယ့် secret နဲ့ permission အပြည့်နဲ့ run သလို ဖြစ်ပါတယ်။

💰 Cost & Cleanup

IAM OIDC provider, IAM role နဲ့ STS call တွေ အခမဲ့ပါ။ GitHub Actions ရဲ့ GitHub-hosted runner minute တွေက public repo အတွက် အခမဲ့ဖြစ်ပြီး private repo အတွက်တော့ plan ရဲ့ လစဉ် ခွဲတမ်းကျော်ရင် ငွေကျပါတယ် (GitHub pricing ကို စစ်ပါ)။ CodeBuild က build minute အလိုက်၊ CodePipeline က active pipeline အလိုက် (သို့) execution အလိုက် ငွေတောင်းပါတယ်။ Pipeline က deploy လုပ်တဲ့ stack တွေကတော့ ယခင်သင်ခန်းစာတွေအတိုင်း ငွေကျပါတယ်။ Lab ပြီးရင် role နဲ့ provider ကို ဖျက်ပါ — အခမဲ့ ဖြစ်ပေမယ့် မသုံးတော့တဲ့ trust relationship ဟာ လုံခြုံရေး အန္တရာယ်ပါ။

စမ်းပြီးရင် ဖျက်ဖို့ command

bash
aws iam delete-role-policy --role-name github-orders-deploy \
  --policy-name assume-cdk-bootstrap-roles
aws iam delete-role --role-name github-orders-deploy
# Only if no other role in this account still uses the GitHub provider:
aws iam delete-open-id-connect-provider --open-id-connect-provider-arn \
  arn:aws:iam::111122223333:oidc-provider/token.actions.githubusercontent.com

Mini challenge

Pull request အတွက် read-only role

Pull request တိုင်းမှာ cdk diff run ဖို့ github-orders-plan ဆိုတဲ့ role အသစ်တစ်ခုအတွက် trust policy ရေးပါ။ ဒီ role က deploy role နဲ့ သီးခြားဖြစ်ပြီး ReadOnlyAccess လို read-only permission ပဲ ရပါမယ်။ တစ်ခုတော့ သတိပြုပါ — cdk diff က deploy လုပ်ထားတဲ့ template ကို CDK bootstrap ရဲ့ lookup role (cdk-hnb659fds-lookup-role-111122223333-*) ကနေတစ်ဆင့် ဖတ်ပါတယ်။ ဒါကြောင့် ဒီ role ရဲ့ permission policy မှာ အဲဒီ lookup role ပေါ်က sts:AssumeRole ကိုလည်း ထည့်ပေးရပါမယ်။ (ဒီ challenge မှာ ရေးရမှာကတော့ trust policy ပါ)

  • Account 111122223333 ရဲ့ GitHub OIDC provider ကို Federated principal အဖြစ် သုံးရမယ်
  • aud ကို sts.amazonaws.com လို့ StringEquals နဲ့ စစ်ရမယ်
  • sub ကို acme/orders-service repo ရဲ့ pull_request event ကိုပဲ အတိအကျ ခွင့်ပြုရမယ်
  • Wildcard (StringLike, *) လုံးဝ မသုံးရ

ရလဒ်: PR workflow ကနေ github-orders-plan ကို assume လုပ်လို့ရပြီး ReadOnlyAccess နဲ့ lookup role ပေါ်က sts:AssumeRole ကြောင့် cdk diff run လို့ရပေမယ့် cdk deploy ကတော့ AccessDenied နဲ့ fail ဖြစ်ပါမယ်။ main ကို push လုပ်တဲ့ workflow ကနေ ဒီ role ကို assume လုပ်ဖို့ ကြိုးစားရင်လည်း sub မကိုက်လို့ ငြင်းပယ်ခံရပါမယ်။

အရိပ်အမြွက် ကြည့်ရန်

pull_request event ရဲ့ sub က repo:OWNER/REPO:pull_request ပုံစံပါ — branch name မပါပါဘူး။ Syntax section ရဲ့ trust policy ကနေ sub တန်ဖိုးတစ်ခုတည်း ပြောင်းရုံပါပဲ။

အဖြေ ကြည့်ရန်
plan-trust-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "GitHubActionsPullRequestPlan",
      "Effect": "Allow",
      "Principal": {
        "Federated":
          "arn:aws:iam::111122223333:oidc-provider/token.actions.githubusercontent.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
          "token.actions.githubusercontent.com:sub":
            "repo:acme/orders-service:pull_request"
        }
      }
    }
  ]
}

Quick Quiz 1 / 3

Trust policy က sub = repo:acme/orders-service:ref:refs/heads/main ကို စစ်ပါတယ်။ main ကို push လုပ်တဲ့ workflow ရဲ့ deploy job မှာ environment: production ထည့်လိုက်တော့ AssumeRoleWithWebIdentity fail ဖြစ်သွားပါတယ်။ ဘာကြောင့်လဲ?

Quick Quiz 2 / 3

CDK app ကို GitHub Actions ကနေ deploy လုပ်တဲ့ OIDC role အတွက် least-privilege ဖြစ်တဲ့ permission က ဘာလဲ?

Quick Quiz 3 / 3

cdk bootstrap aws://444455556666/us-east-1 --trust 111122223333 က ဘာလုပ်ပေးလဲ?

မှတ်ထားရမယ့် အချက်များ

  • CI ထဲက long-lived access key က သက်တမ်းမကုန်ဘဲ ယိုစိမ့်ရင် ဘယ်ကမဆို သုံးလို့ရပါတယ် — OIDC နဲ့ run တစ်ခုချင်းအတွက် temporary credential ကို ပြောင်းသုံးပါ။
  • IAM OIDC provider + sts:AssumeRoleWithWebIdentity role + aud နဲ့ sub ကို StringEquals နဲ့ အတိအကျ စစ်ပါ။
  • Workflow မှာ id-token: write ကို deploy job မှာပဲ ပေးပြီး configure-aws-credentials ရဲ့ role-to-assume ကို သုံးပါ။
  • CDK အတွက် CI role ကို bootstrap role တွေ assume လုပ်ခွင့်ပဲ ပေးပြီး cross-account ကို --trust (သို့) account အလိုက် role နဲ့ လုပ်ပါ။
  • Approval gate ကို GitHub environment required reviewer (သို့) CodePipeline manual approval နဲ့ ထားပါ။

IAM: Create an OpenID Connect (OIDC) identity provider — AWS Documentation

IAM: Create a role for OpenID Connect federation — AWS Documentation

AWS CodePipeline User Guide — AWS Documentation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

OIDC နဲ့ CI/CD — Access key မပါဘဲ GitHub Actions ကနေ AWS ကို deploy လုပ်ခြင်း | Thuta Learning