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

To Nha Notes | March 19, 2021, 5:08 p.m.

We can perform below configures via AWS console or awscli v2.

To install awscli v2:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install -i /usr/local/aws-cli -b /usr/local/bin
1. Create a target group with the target type set to Lambda:
aws elbv2 create-target-group \
    --name maintenance-lambda-<ENV_NAME> \
    --target-type lambda \
    --profile <YOUR_AWS_PROFILE>
2. Add permission to allow ALB to invoke Lambda function
aws lambda add-permission \
    --function-name <YOUR_MAINTENANCE_LAMBDA_NAME> \
    --statement-id load-balancer \
    --principal elasticloadbalancing.amazonaws.com \
    --action lambda:InvokeFunction \
    --source-arn <TARGET_GROUP_ARN_CREATED_IN_STEP_1> \
    --profile <YOUR_AWS_PROFILE>

See here for more information