Skip to content

improv(ci): automated the Gamma/Prod deployment to China/GovCloud regions - #4864

Merged
sdangol merged 4 commits into
mainfrom
improv/layer-partition
Dec 12, 2025
Merged

sdangol merged 4 commits into
mainfrom
improv/layer-partition

Conversation

@sdangol

@sdangol sdangol commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

Summary

This PR updates the Layer Deployment (Partitions) workflow to automate the step of first deploying the layer to gamma and then to prod. It also removes the need to input the layer version number manually in the workflow

Changes

  • Removed the environment and version number input from the workflow
  • Added a step to get the latest layer version number from us-east-1
  • Created a reusable workflow to deploy the layer
  • Updated the workflow to deploy to gamma and then to prod automatically

Issue number: closes #4858


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg Bot added the automation This item relates to automation label Dec 12, 2025
@pull-request-size pull-request-size Bot added the size/L PRs between 100-499 LOC label Dec 12, 2025
@sdangol sdangol self-assigned this Dec 12, 2025
@sdangol
sdangol requested a review from svozza December 12, 2025 11:23
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708
with:
role-to-assume: ${{ secrets[format('IAM_ROLE_{0}', steps.transform.outputs.CONVERTED_REGION)] }}

Check warning

Code scanning / CodeQL

Excessive Secrets Exposure

All organization and repository secrets are passed to the workflow runner in [secrets\[format('IAM_ROLE_{0}', steps.transform.outputs.CONVERTED_REGION)\]](1)

Copilot Autofix

AI 12 months ago

General Fix Approach:
The correct and secure approach is to avoid dynamically referencing secrets at runtime. Instead, enumerate all possible secrets needed, and explicitly reference them in the workflow with a static name. This is typically achieved using if conditionals within the job or step definitions to select the specific secret based on the partition or region.

Detailed Best Fix:

  1. Identify all possible values of CONVERTED_REGION that would be produced (matrix.region mapped from input) and what IAM role secret is needed for each.
  2. For each possible region, create a separate step (or preferably, a job-level env) with a conditional if, setting the specific role-to-assume as a static reference: e.g., secrets.IAM_ROLE_US_EAST_1.
  3. This may be verbose if there are many regions, but it's the only way to avoid passing all secrets.
  4. The steps file to edit is .github/workflows/layers_partitions_deploy.yml, specifically the Configure AWS Credentials step (lines 54-61).

Implementation Needs:

  • The Configure AWS Credentials step must be replaced by multiple steps—one for each region, each with a static secrets reference and an if clause.
  • If you want to avoid duplicating the rest of the deployment process, you could instead set an environment variable or output in a prior step depending on the region and use that value. However, since the secret must be referenced statically, the direct per-region-step approach is best here.
  • No new imports or packages needed; just YAML editing.

Suggested changeset 1
.github/workflows/layers_partitions_deploy.yml

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/.github/workflows/layers_partitions_deploy.yml b/.github/workflows/layers_partitions_deploy.yml
--- a/.github/workflows/layers_partitions_deploy.yml
+++ b/.github/workflows/layers_partitions_deploy.yml
@@ -51,14 +51,26 @@
         run: |
           echo 'CONVERTED_REGION=${{ matrix.region }}' | tr 'a-z\-' 'A-Z_' >> "$GITHUB_OUTPUT"
       
-      - name: Configure AWS Credentials
+      # BEGIN: Configure AWS Credentials by region with static secret references
+      - name: Configure AWS Credentials (us-east-1)
+        if: matrix.region == 'us-east-1'
         uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708 # v5.1.1
         with:
-          role-to-assume: ${{ secrets[format('IAM_ROLE_{0}', steps.transform.outputs.CONVERTED_REGION)] }}
-          aws-region: ${{ matrix.region }}
+          role-to-assume: ${{ secrets.IAM_ROLE_US_EAST_1 }}
+          aws-region: us-east-1
           mask-aws-account-id: true
           audience: ${{ inputs.aud }}
       
+      - name: Configure AWS Credentials (eu-west-1)
+        if: matrix.region == 'eu-west-1'
+        uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708 # v5.1.1
+        with:
+          role-to-assume: ${{ secrets.IAM_ROLE_EU_WEST_1 }}
+          aws-region: eu-west-1
+          mask-aws-account-id: true
+          audience: ${{ inputs.aud }}
+      
+      # END: Configure AWS Credentials by region
       - name: Create Layer
         id: create-layer
         run: |
EOF
@@ -51,14 +51,26 @@
run: |
echo 'CONVERTED_REGION=${{ matrix.region }}' | tr 'a-z\-' 'A-Z_' >> "$GITHUB_OUTPUT"

- name: Configure AWS Credentials
# BEGIN: Configure AWS Credentials by region with static secret references
- name: Configure AWS Credentials (us-east-1)
if: matrix.region == 'us-east-1'
uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708 # v5.1.1
with:
role-to-assume: ${{ secrets[format('IAM_ROLE_{0}', steps.transform.outputs.CONVERTED_REGION)] }}
aws-region: ${{ matrix.region }}
role-to-assume: ${{ secrets.IAM_ROLE_US_EAST_1 }}
aws-region: us-east-1
mask-aws-account-id: true
audience: ${{ inputs.aud }}

- name: Configure AWS Credentials (eu-west-1)
if: matrix.region == 'eu-west-1'
uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708 # v5.1.1
with:
role-to-assume: ${{ secrets.IAM_ROLE_EU_WEST_1 }}
aws-region: eu-west-1
mask-aws-account-id: true
audience: ${{ inputs.aud }}

# END: Configure AWS Credentials by region
- name: Create Layer
id: create-layer
run: |
Copilot is powered by AI and may make mistakes. Always verify output.
svozza
svozza previously approved these changes Dec 12, 2025
@sdangol sdangol added the do-not-merge This item should not be merged label Dec 12, 2025
@sonarqubecloud

Copy link
Copy Markdown

set -euo pipefail
export layer_output="AWSLambdaPowertoolsTypeScriptV2-${{ matrix.region }}.json"
# Dynamic secret access is safe here - secrets are scoped per environment
aws --region "${{ matrix.region }}" lambda get-layer-version-by-arn --arn "arn:${PARTITION}:lambda:${{ matrix.region }}:${{ secrets[format('AWS_ACCOUNT_{0}', steps.transform.outputs.CONVERTED_REGION)] }}:layer:AWSLambdaPowertoolsTypeScriptV2:${LAYER_VERSION}" > "$layer_output"

Check warning

Code scanning / CodeQL

Excessive Secrets Exposure

All organization and repository secrets are passed to the workflow runner in [secrets\[format('AWS_ACCOUNT_{0}', steps.transform.outputs.CONVERTED_REGION)\]](1)

Copilot Autofix

AI 12 months ago

General fix:
Replace dynamic secret access with explicit, per-region secret mapping.
Detailed fix:
Instead of using ${{ secrets[format('AWS_ACCOUNT_{0}', steps.transform.outputs.CONVERTED_REGION)] }} inside the bash script, leverage the workflow matrix to enumerate all the regions you deploy to and in the job definition, emit the relevant account ID as a job/environment variable—this allows you to reference the relevant secret directly by name, without dynamic runtime lookup.

How:

  • In your workflow matrix or before this step, add a new output or environment variable, say AWS_ACCOUNT_ID, and set it as follows for each valid region:
    If you have e.g. AWS_ACCOUNT_us_east_1 and AWS_ACCOUNT_eu_west_1 secrets, you can map region name to the corresponding secret during workflow definition, using if conditionals or environment with static references.
  • Then, pass just that one explicitly referenced secret to the workflow, e.g.:
    env:
      AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_US_EAST_1 }}
    or for multiple:
    jobs:
      ...
        environment: ...
        strategy:
          matrix:
            region: [us-east-1, eu-west-1]
        steps:
          - name: Set Account ID
            id: set-account
            run: echo "AWS_ACCOUNT_ID=${{ secrets.AWS_ACCOUNT_US_EAST_1 }}" >> $GITHUB_ENV
            if: matrix.region == 'us-east-1'
          - name: Set Account ID
            id: set-account
            run: echo "AWS_ACCOUNT_ID=${{ secrets.AWS_ACCOUNT_EU_WEST_1 }}" >> $GITHUB_ENV
            if: matrix.region == 'eu-west-1'
    Then use just ${AWS_ACCOUNT_ID} in your shell command.

Changes needed in .github/workflows/layers_partitions_deploy.yml:

  • For each region, set the relevant secret statically by name before the verify step and export it as an environment variable.
  • Update the shell script in the "Verify Layer" step to use $AWS_ACCOUNT_ID in place of the dynamic ${{ secrets[...] }}.

Suggested changeset 1
.github/workflows/layers_partitions_deploy.yml

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/.github/workflows/layers_partitions_deploy.yml b/.github/workflows/layers_partitions_deploy.yml
--- a/.github/workflows/layers_partitions_deploy.yml
+++ b/.github/workflows/layers_partitions_deploy.yml
@@ -90,11 +90,12 @@
           LAYER_VERSION: ${{ steps.create-layer.outputs.LAYER_VERSION }}
           ENVIRONMENT: ${{ inputs.environment }}
           PARTITION: ${{ inputs.arn_partition }}
+          # Set AWS_ACCOUNT_ID explicitly per region
+          AWS_ACCOUNT_ID: ${{ (matrix.region == 'us-east-1' && secrets.AWS_ACCOUNT_US_EAST_1) || (matrix.region == 'eu-west-1' && secrets.AWS_ACCOUNT_EU_WEST_1) || (matrix.region == 'ap-south-1' && secrets.AWS_ACCOUNT_AP_SOUTH_1) }}
         run: |
           set -euo pipefail
           export layer_output="AWSLambdaPowertoolsTypeScriptV2-${{ matrix.region }}.json"
-          # Dynamic secret access is safe here - secrets are scoped per environment
-          aws --region "${{ matrix.region }}" lambda get-layer-version-by-arn --arn "arn:${PARTITION}:lambda:${{ matrix.region }}:${{ secrets[format('AWS_ACCOUNT_{0}', steps.transform.outputs.CONVERTED_REGION)] }}:layer:AWSLambdaPowertoolsTypeScriptV2:${LAYER_VERSION}" > "$layer_output"
+          aws --region "${{ matrix.region }}" lambda get-layer-version-by-arn --arn "arn:${PARTITION}:lambda:${{ matrix.region }}:${AWS_ACCOUNT_ID}:layer:AWSLambdaPowertoolsTypeScriptV2:${LAYER_VERSION}" > "$layer_output"
           
           REMOTE_SHA=$(jq -r '.Content.CodeSha256' "$layer_output")
           LOCAL_SHA=$(jq -r '.Content.CodeSha256' AWSLambdaPowertoolsTypeScriptV2.json)
EOF
@@ -90,11 +90,12 @@
LAYER_VERSION: ${{ steps.create-layer.outputs.LAYER_VERSION }}
ENVIRONMENT: ${{ inputs.environment }}
PARTITION: ${{ inputs.arn_partition }}
# Set AWS_ACCOUNT_ID explicitly per region
AWS_ACCOUNT_ID: ${{ (matrix.region == 'us-east-1' && secrets.AWS_ACCOUNT_US_EAST_1) || (matrix.region == 'eu-west-1' && secrets.AWS_ACCOUNT_EU_WEST_1) || (matrix.region == 'ap-south-1' && secrets.AWS_ACCOUNT_AP_SOUTH_1) }}
run: |
set -euo pipefail
export layer_output="AWSLambdaPowertoolsTypeScriptV2-${{ matrix.region }}.json"
# Dynamic secret access is safe here - secrets are scoped per environment
aws --region "${{ matrix.region }}" lambda get-layer-version-by-arn --arn "arn:${PARTITION}:lambda:${{ matrix.region }}:${{ secrets[format('AWS_ACCOUNT_{0}', steps.transform.outputs.CONVERTED_REGION)] }}:layer:AWSLambdaPowertoolsTypeScriptV2:${LAYER_VERSION}" > "$layer_output"
aws --region "${{ matrix.region }}" lambda get-layer-version-by-arn --arn "arn:${PARTITION}:lambda:${{ matrix.region }}:${AWS_ACCOUNT_ID}:layer:AWSLambdaPowertoolsTypeScriptV2:${LAYER_VERSION}" > "$layer_output"

REMOTE_SHA=$(jq -r '.Content.CodeSha256' "$layer_output")
LOCAL_SHA=$(jq -r '.Content.CodeSha256' AWSLambdaPowertoolsTypeScriptV2.json)
Copilot is powered by AI and may make mistakes. Always verify output.
@sdangol sdangol removed the do-not-merge This item should not be merged label Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation This item relates to automation size/L PRs between 100-499 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Maintenance: Improve Govcloud/China Deployment Github Action

3 participants