配置ALB / Lambda和目标组的问题通常涉及以下方面:设置ALB、创建Lambda函数、创建目标组以及配置ALB与Lambda函数之间的关联。下面是一个示例解决方法,包含代码示例来说明每个步骤。
import boto3
client = boto3.client('elbv2')
response = client.create_load_balancer(
Name='my-alb',
Subnets=[
'subnet-xxxxxxxx',
'subnet-xxxxxxxx',
],
SecurityGroups=[
'sg-xxxxxxxx',
],
Type='application',
Scheme='internet-facing',
)
import boto3
client = boto3.client('lambda')
response = client.create_function(
FunctionName='my-lambda',
Runtime='python3.9',
Role='arn:aws:iam::xxxxxxxxxxxx:role/lambda-role',
Handler='lambda_function.lambda_handler',
Code={
'ZipFile': b'zip-file-content',
},
)
import boto3
client = boto3.client('elbv2')
response = client.create_target_group(
Name='my-target-group',
Protocol='HTTP',
Port=80,
VpcId='vpc-xxxxxxxx',
)
import boto3
client = boto3.client('elbv2')
response = client.create_listener(
LoadBalancerArn='arn:aws:elasticloadbalancing:region:xxxxxxxxxxxx:loadbalancer/my-alb',
Protocol='HTTP',
Port=80,
DefaultActions=[
{
'Type': 'forward',
'TargetGroupArn': 'arn:aws:elasticloadbalancing:region:xxxxxxxxxxxx:targetgroup/my-target-group',
},
],
)
以上是一个基本的解决方法,用于配置ALB / Lambda和目标组。请根据您的实际需求进行适当调整。