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

Last updated: 2020-04-15

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

1.    Confirm that you're running the most recent version of the AWS CLI. If necessary, upgrade your AWS CLI to the latest version.

2.    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

3.    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)] 

4.    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 respective ARNs.

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

Did this article help you?

Anything we could improve?


Need more help?