Repository navigation
improv(ci): automated the Gamma/Prod deployment to China/GovCloud regions - #4864
Conversation
| - 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
Show autofix suggestion
Hide autofix suggestion
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:
- Identify all possible values of
CONVERTED_REGIONthat would be produced (matrix.regionmapped from input) and what IAM role secret is needed for each. - For each possible region, create a separate step (or preferably, a job-level
env) with a conditionalif, setting the specificrole-to-assumeas a static reference: e.g.,secrets.IAM_ROLE_US_EAST_1. - This may be verbose if there are many regions, but it's the only way to avoid passing all secrets.
- The steps file to edit is
.github/workflows/layers_partitions_deploy.yml, specifically theConfigure AWS Credentialsstep (lines 54-61).
Implementation Needs:
- The
Configure AWS Credentialsstep must be replaced by multiple steps—one for each region, each with a static secrets reference and anifclause. - 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.
| @@ -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: | |
|
| 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
Show autofix suggestion
Hide autofix suggestion
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_1andAWS_ACCOUNT_eu_west_1secrets, you can map region name to the corresponding secret during workflow definition, usingifconditionals orenvironmentwith static references. - Then, pass just that one explicitly referenced secret to the workflow, e.g.:
or for multiple:
env: AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_US_EAST_1 }}
Then use justjobs: ... 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'
${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_IDin place of the dynamic${{ secrets[...] }}.
| @@ -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) |



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
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.