-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1936 from ASFHyP3/develop
Release v4.3.2
- Loading branch information
Showing
7 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
AWSTemplateFormatVersion: 2010-09-09 | ||
|
||
Parameters: | ||
VpcId: | ||
Type: AWS::EC2::VPC::Id | ||
|
||
SecurityGroupId: | ||
Type: String | ||
|
||
SubnetIds: | ||
Type: CommaDelimitedList | ||
|
||
Resources: | ||
|
||
LogGroup: | ||
Type: AWS::Logs::LogGroup | ||
Properties: | ||
LogGroupName: !Sub "/aws/lambda/${Lambda}" | ||
RetentionInDays: 90 | ||
|
||
Role: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
Action: sts:AssumeRole | ||
Principal: | ||
Service: lambda.amazonaws.com | ||
Effect: Allow | ||
ManagedPolicyArns: | ||
- !Ref Policy | ||
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole | ||
|
||
Policy: | ||
Type: AWS::IAM::ManagedPolicy | ||
Properties: | ||
PolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- Effect: Allow | ||
Action: | ||
- logs:CreateLogStream | ||
- logs:PutLogEvents | ||
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*" | ||
- Effect: Allow | ||
Action: | ||
- ec2:DescribeVpcEndpoints | ||
- ec2:ModifyVpcEndpoint | ||
Resource: "*" | ||
|
||
Lambda: | ||
Type: AWS::Lambda::Function | ||
Properties: | ||
Code: src/ | ||
Handler: disable_private_dns.lambda_handler | ||
MemorySize: 128 | ||
Role: !GetAtt Role.Arn | ||
Runtime: python3.9 | ||
Timeout: 5 | ||
Environment: | ||
Variables: | ||
VPCID: !Ref VpcId | ||
ENDPOINT_NAME: "VPC Endpoint - Consumer" | ||
VpcConfig: | ||
SecurityGroupIds: | ||
- !Ref SecurityGroupId | ||
SubnetIds: !Ref SubnetIds | ||
|
||
EventInvokeConfig: | ||
Type: AWS::Lambda::EventInvokeConfig | ||
Properties: | ||
FunctionName: !Ref Lambda | ||
Qualifier: $LATEST | ||
MaximumRetryAttempts: 0 | ||
|
||
Schedule: | ||
Type: AWS::Events::Rule | ||
Properties: | ||
ScheduleExpression: "rate(1 minute)" | ||
Targets: | ||
- Arn: !GetAtt Lambda.Arn | ||
Id: lambda | ||
|
||
EventPermission: | ||
Type: AWS::Lambda::Permission | ||
Properties: | ||
FunctionName: !GetAtt Lambda.Arn | ||
Action: lambda:InvokeFunction | ||
Principal: events.amazonaws.com | ||
SourceArn: !GetAtt Schedule.Arn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
|
||
import boto3 | ||
|
||
|
||
CLIENT = boto3.client('ec2') | ||
|
||
|
||
def get_endpoint(vpc_id, endpoint_name): | ||
response = CLIENT.describe_vpc_endpoints() | ||
endpoints = [endpoint for endpoint in response['VpcEndpoints'] if endpoint['VpcId'] == vpc_id] | ||
if len(endpoints) == 0: | ||
raise ValueError(f'No endpoints in VPC {vpc_id}.') | ||
|
||
desired_endpoint = None | ||
for endpoint in endpoints: | ||
retrieved_name = [item['Value'] for item in endpoint['Tags'] if item['Key'] == 'Name'][0] | ||
if retrieved_name == endpoint_name: | ||
desired_endpoint = endpoint | ||
|
||
if desired_endpoint is None: | ||
raise ValueError(f'No endpoint in VPC {vpc_id} with name {endpoint_name} exists.') | ||
|
||
return desired_endpoint | ||
|
||
|
||
def set_private_dns_disabled(endpoint_id): | ||
response = CLIENT.modify_vpc_endpoint(VpcEndpointId=endpoint_id, PrivateDnsEnabled=False) | ||
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/modify_vpc_endpoint.html | ||
assert response['Return'] is True, response | ||
print(f"Private DNS disabled for VPC Endpoint: {endpoint_id}.") | ||
|
||
|
||
def disable_private_dns(vpc_id, endpoint_name): | ||
endpoint = get_endpoint(vpc_id, endpoint_name) | ||
if endpoint['PrivateDnsEnabled']: | ||
print(f"Private DNS enabled for VPC Endpoint: {endpoint['VpcEndpointId']}, changing...") | ||
set_private_dns_disabled(endpoint['VpcEndpointId']) | ||
else: | ||
print(f"Private DNS already disabled for VPC Endpoint: {endpoint['VpcEndpointId']}, doing nothing.") | ||
|
||
|
||
def lambda_handler(event, context): | ||
vpc_id = os.environ['VPCID'] | ||
endpoint_name = os.environ['ENDPOINT_NAME'] | ||
print(f'VPC ID {vpc_id}') | ||
print(f'Endpoint Name: {endpoint_name}') | ||
disable_private_dns(vpc_id, endpoint_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
boto3==1.28.82 |