How can I use the AWS CLI to register a Lambda function as a target behind my Application Load Balancer?

2 minute read
0

I want to register a Lambda function as a target behind my Application Load Balancer using the AWS Command Line Interface (AWS CLI). How can I do this?

Resolution

Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent AWS CLI version.

1.    Create a target group with the target type set to Lambda. Be sure to replace [your target group name] with the name of your target.

aws elbv2 create-target-group \
    --name [your target group's name] \
    --target-type lambda

2.    Use the add-permission command to allow Elastic Load Balancing to invoke your Lambda function. Be sure to replace [your Lambda function's name] and [your target group's Amazon Resource Name (ARN)] with your respective names.

aws lambda add-permission \
    --function-name [your Lambda function's name] \
    --statement-id load-balancer \
    --principal elasticloadbalancing.amazonaws.com \
    --action lambda:InvokeFunction \
    --source-arn [your target group's Amazon Resource Name (ARN)]

3.    Use the register-targets command to register Lambda as the target. Be sure to replace [your target group's ARN] and **[your Lambda function's ARN]**with your ARNs.

aws elbv2 register-targets \ 
    --target-group-arn [your target group's ARN] \ 
    --targets Id=[your Lambda function's ARN]

AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago