agent: |
jS78oQ0GhtZ2eexQGvatModify AWS ALB Listeners to Enforce HTTPS Redirection
Modify AWS ALB Listeners to Enforce HTTPS Redirection
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This procedure adjusts AWS ALB listeners to ensure that all HTTP traffic is automatically redirected to HTTPS, enhancing the security of data transmission by using encryption.
inputs
outputs
import boto3
from botocore.exceptions import ClientError
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
'''
# Example structure, list passed down from upstream task
listeners_without_redirect = [
{'Region': 'us-east-1', 'ALBName': 'alb-blahblah', 'ListenerArn': 'arn:aws:elasticloadbalancing:us-east-1:355237452254:listener/app/alb-blahblah/599c713a19a4afce/56d57ff44837d30f'},
{'Region': 'us-east-1', 'ALBName': 'alb-blahblah', 'ListenerArn': 'arn:aws:elasticloadbalancing:us-east-1:355237452254:listener/app/alb-blahblah/599c713a19a4afce/6a54c2e7d3c08fee'}
]
'''
# Check if there are any listeners to process
if not listeners_without_redirect:
print("No listeners provided to modify. Exiting task.")
else:
print(f"Attempting to modify {len(listeners_without_redirect)} listener(s) to enforce HTTP redirection...")
for listener_info in listeners_without_redirect:
region = listener_info['Region']
listener_arn = listener_info['ListenerArn']
elbv2_client = boto3.client('elbv2', aws_access_key_id=access_key,aws_secret_access_key=secret_key,region_name=region)
try:
elbv2_client.modify_listener(
ListenerArn=listener_arn,
DefaultActions=[
{
'Type': 'redirect',
'Order': 1,
'RedirectConfig': {
'Protocol': 'HTTPS',
'Port': '443',
'Host': '#{host}',
'Path': '/#{path}',
'Query': '#{query}',
'StatusCode': 'HTTP_301'
}
}
]
)
print(f"Modified listener {listener_arn} in ALB {listener_info['ALBName']} to enforce HTTP redirection.")
except ClientError as e:
print(f"Error modifying listener {listener_arn} in ALB {listener_info['ALBName']} in region {region}: {e}")
print("Modification task completed.")
copied