Skip to content

Commit 4cabbd6

Browse files
authored
Merge pull request #163 from Boxuan996/boxuan-replace-user-with-role
Replace User creds with Role creds for CWL agent
2 parents 8770280 + 4ff5e6b commit 4cabbd6

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

bin/start-agent.sh

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,41 @@ tempfile="$rootdir/src/integration-test/resources/agent/.temp"
1818
# Configure and start the agent
1919
###################################
2020

21-
pushd $rootdir/src/integration-test/resources/agent
22-
echo "[AmazonCloudWatchAgent]
21+
# Check if IAM user credentials exist
22+
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
23+
echo "No IAM user credentials found, assuming we are running on CodeBuild pipeline, falling back to IAM role.."
24+
25+
# Store the AWS STS assume-role output and extract credentials
26+
CREDS=$(aws sts assume-role \
27+
--role-arn $Code_Build_Execution_Role_ARN \
28+
--role-session-name "session-$(uuidgen)" \
29+
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \
30+
--output text \
31+
--duration-seconds 3600)
32+
33+
# Parse the output into separate variables
34+
read AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN <<< $CREDS
35+
36+
# Export the variables
37+
export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
38+
39+
CREDENTIALS_CONTENT="[AmazonCloudWatchAgent]
2340
aws_access_key_id = $AWS_ACCESS_KEY_ID
2441
aws_secret_access_key = $AWS_SECRET_ACCESS_KEY
25-
" > ./.aws/credentials
42+
aws_session_token = $AWS_SESSION_TOKEN"
43+
else
44+
echo "Using provided IAM user credentials..."
45+
CREDENTIALS_CONTENT="[AmazonCloudWatchAgent]
46+
aws_access_key_id = $AWS_ACCESS_KEY_ID
47+
aws_secret_access_key = $AWS_SECRET_ACCESS_KEY"
48+
fi
49+
50+
pushd $rootdir/src/integration-test/resources/agent
51+
52+
# Write credentials file
53+
echo "$CREDENTIALS_CONTENT" > ./.aws/credentials
2654

55+
# Write config file
2756
echo "[profile AmazonCloudWatchAgent]
2857
region = $AWS_REGION
2958
" > ./.aws/config

0 commit comments

Comments
 (0)