AWS CodePipeline:如何将源操作提交信息传递至手动审批CustomData字段?
CustomData field in AWS CodePipeline? Absolutely! You can absolutely pass trigger details like commit messages from a Source Action to the CustomData field of a Manual Approval action in AWS CodePipeline—you just need a small Lambda function as a middleman to fetch, format, and pass that data along. Here's a step-by-step breakdown of how to make this work:
Step 1: Create a Lambda Function to Process Trigger Data
This function will grab the commit ID (or other source trigger details) from your pipeline's source stage, fetch the corresponding commit message from your repo, format it, and pass it as an output variable to the pipeline.
Here's a Python example tailored for CodeCommit (adjust for GitHub/GitLab as needed):
import boto3 import json codepipeline = boto3.client('codepipeline') codecommit = boto3.client('codecommit') def lambda_handler(event, context): # Extract job and pipeline details from the CodePipeline event job_id = event['CodePipeline.job']['id'] job_data = event['CodePipeline.job']['data'] pipeline_name = job_data['pipelineName'] execution_id = job_data['pipelineExecutionId'] # Find the Source Action's execution details to get the commit ID execution_details = codepipeline.get_pipeline_execution( pipelineName=pipeline_name, pipelineExecutionId=execution_id ) # Replace 'Source' with your actual Source Action name source_action = next( action for action in execution_details['pipelineExecution']['stageStates'][0]['actionStates'] if action['actionName'] == 'Source' ) commit_id = source_action['actionExecution']['output']['outputVariables']['CommitId'] # Fetch the full commit details (including message) from CodeCommit commit_details = codecommit.get_commit( repositoryName='YOUR_REPO_NAME', commitId=commit_id ) commit_message = commit_details['commit']['message'] # Format the content you want to show in CustomData formatted_custom_data = f""" Commit Message: {commit_message} Commit ID: {commit_id} Pipeline Execution ID: {execution_id} """.strip() # Pass the formatted data as an output variable back to CodePipeline codepipeline.put_job_success_result( jobId=job_id, outputVariables={ 'ApprovalCustomData': formatted_custom_data } )
IAM Permissions for the Lambda
Make sure your Lambda's execution role has these permissions:
codepipeline:GetPipelineExecutioncodepipeline:PutJobSuccessResultcodecommit:GetCommit(if using CodeCommit; for GitHub, add logic to use a PAT and call their API instead)
Step 2: Update Your CodePipeline
Add a new Stage right after your Source Stage (name it something like
ProcessTriggerData).Add a Lambda Action to this new stage, pointing to the function you created.
Edit your Manual Approval Action: In the
CustomDatafield, reference the Lambda's output variable using CodePipeline's variable syntax. For example:#{ProcessTriggerData.YourLambdaActionName.ApprovalCustomData}Replace
YourLambdaActionNamewith the name you gave the Lambda Action in your pipeline.Double-check that your Approval Action's
NotificationArnis set to your SNS Topic with email subscriptions—subscribers will now see the formatted commit details directly in their approval/rejection emails.
Key Notes
- GitHub/GitLab Adjustments: If you're using GitHub, you'll need to use a Personal Access Token (store it in Secrets Manager) and call the GitHub API (
GET /repos/{owner}/{repo}/commits/{sha}) to fetch the commit message instead of using CodeCommit's API. - Variable Size Limits: CodePipeline output variables are limited to 1024 characters each, so truncate long commit messages if needed to avoid errors.
- Action Names: Make sure to use the exact names of your Source Action and Lambda Action in the code and pipeline variable reference—typos will break the flow!
内容的提问来源于stack exchange,提问作者kiran01bm

