Skip to content

Instantly share code, notes, and snippets.

@joekiller
Last active June 25, 2020 19:09
Show Gist options
  • Save joekiller/ffddfe4942c09d4c9e5bc507aacc1076 to your computer and use it in GitHub Desktop.
Save joekiller/ffddfe4942c09d4c9e5bc507aacc1076 to your computer and use it in GitHub Desktop.
Enable Lambda X-Ray on all functions via AWS CLI
#!/bin/bash
fns=($(aws lambda list-functions --query "Functions[].FunctionName" --output text))
GrantWrite () {
aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess --role-name $(ROLE=$(aws lambda get-function --function-name $1 --query "Configuration.Role" --output text);echo ${ROLE##*/})
}
XRay () {
aws lambda update-function-configuration --function-name $1 --tracing-config Mode=Active >/dev/null && echo $1 OK || (GrantWrite $1; aws lambda update-function-configuration --function-name $1 --tracing-config Mode=Active > /dev/null && echo $1 OK || echo $1 FAILED)
}
for f in ${fns[@]}; do XRay $f; done
@gvasquez95
Copy link

Seems like it's missing some kind of "wait state" between the grant and the lambda update, as the permissions don't get propagated to the lambdas right away, but if you just wait (or run the script twice) it succeeds, otherwise you get errors like:

An error occurred (InvalidParameterValueException) when calling the UpdateFunctionConfiguration operation: The provided execution role does not have permissions to call PutTraceSegments on XRAY

@joekiller
Copy link
Author

@gvasquez-waypoint I agree, IAM can have a slight lag on associate policy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment