- Newest
- Most votes
- Most comments
As far as I understand the issue, the previous answer regarding IAM permissions is a good first step, but it likely doesn't address the root cause here. This is a known architectural limitation in AWS CodeBuild when operating inside a VPC with a mandatory proxy.
The Problem: Lifecycle Timing
The issue is the sequence of events during the CodeBuild provisioning process:
- Container Provisioning: CodeBuild starts the executor.
- Environment Initialization: This is where the agent attempts to set up the network.
- DOWNLOAD_SOURCE Phase: The agent attempts to clone the repository from GitHub.
- Secret Resolution: CodeBuild resolves environment variables of type
SECRETS_MANAGER.
When you define https_proxy as type SECRETS_MANAGER, the value is often not resolved and injected into the shell environment early enough for the git client to use it during the DOWNLOAD_SOURCE phase. Because the build is in a VPC and requires a proxy to reach GitHub, the git clone fails before the agent has even "fetched" the secret value from the Secrets Manager service.
Why PLAINTEXT and Dynamic References work
- PLAINTEXT: The value is available immediately upon container start.
- CloudFormation Dynamic References
({{resolve:secretsmanager:...}}): These are resolved by CloudFormation at deployment time. The actual string is passed to the CodeBuild Project configuration as a static value. Therefore, the agent knows the proxy credentials the moment it initializes, allowing the source download to succeed.
Your current workaround using CloudFormation Dynamic References is actually the standard industry fix for this specific "bootstrap" problem as far as I know.
Keep in mind:
- Rotation: If you use Dynamic References, CodeBuild will not see an updated password if the secret is rotated in Secrets Manager until you re-deploy the CloudFormation stack.
- Security: While this is more secure than hardcoding, the resolved secret will be visible in the CodeBuild console's "Environment" tab unless you take extra precautions.
see also:
-
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html
-
Explicit proxy: If you use an explicit proxy server, you must configure NO_PROXY, HTTP_PROXY, and HTTPS_PROXY environment variables in CodeBuild at the project level. For more information, see Change build project settings in AWS CodeBuild and Create a build project in AWS CodeBuild. source -> https://docs.aws.amazon.com/codebuild/latest/userguide/use-proxy-server.html
Alternative: If you need to handle rotation without re-deploying, the only other way is to move the source download out of the DOWNLOAD_SOURCE phase (by using NO_SOURCE and manually performing a git clone in the PRE_BUILD phase after manually fetching the secret via AWS CLI), but this is significantly more complex and loses the native Webhook integration.
Sounds like the CodeBuild role doesnt have IAM permissions to get Secret or Decrypt Secret. You can use Cloudtrail to look for Error messages which would point to the issue. If you compare existing to your new one, check IAM Permissions and KMS Key used by the Secret as the CFN Template doesnt uncover that part of your solution
Thank you for your comment
Relevant content
- asked 4 years ago

Thank you very much for your comprehensive explanation.