鉴于Cloudformation目前不支持ALB Lambda集成,我正在尝试编写一个简单的脚本来创建目标组,将lambda注册到目标组,然后将侦听器规则指向该目标组。
当我通过用户界面注册lambda目标时,这是有效的,但是我尝试将lambda目标注册到目标组的尝试失败了(在python脚本和cli中):
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the RegisterTargets operation: elasticloadbalancing principal does not have permission to invoke <LAMBDA ARN> from target group <TARGET GROUP ARN>以下是执行此操作的python脚本:
import boto3
import os
environment = os.environ['ENV']
cloudformation = boto3.resource('cloudformation')
elb = boto3.client('elbv2')
stack = cloudformation.Stack('boomerang')
output = [x for x in stack.outputs if x['ExportName'] == 'boomerang-beacon-lambda'][0]
beacon_arn = output['OutputValue']
response = elb.create_target_group(
TargetType='lambda',
Name='public-%s-boomerang-beacon' % environment
)
target_group_arn = response['TargetGroups'][0]['TargetGroupArn']
elb.register_targets(
TargetGroupArn=target_group_arn,
Targets=[
{
'Id': beacon_arn
},
]
)谢谢
发布于 2019-01-26 04:00:39
您必须创建一个添加lambda函数的权限,以允许弹性负载平衡主体调用您的lambda函数。使用CloudFormation,您可以添加以下资源以使其工作。
LambdaFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt LambdaTargetFunction.Arn
Principal: elasticloadbalancing.amazonaws.com
SourceArn: !Ref TargetGroup有关Lambda添加权限功能的更多信息可在此处找到:https://docs.aws.amazon.com/lambda/latest/dg/API_AddPermission.html
发布于 2021-03-05 18:08:24
'''
Below function uses boto3 to add permission to lambda function and then register it to TargetGroup
'''
def addLambdaPermission():
clientLambda = boto3.client('lambda',aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY,region_name=REGION)
response = clientLambda.add_permission(
Action='lambda:InvokeFunction',
FunctionName=lambda_name,
Principal='elasticloadbalancing.amazonaws.com',
StatementId='registerTargetPermission',
)
print('Permissions added')
addLambdaPermission()
response = client.register_targets(
TargetGroupArn= TGArn,
Targets=[
{
'Id': lambda_arn,
},
],
)
print('Lambda registered with TG')https://stackoverflow.com/questions/54072326
复制相似问题