|
| 1 | +AWSTemplateFormatVersion: 2010-09-09 |
| 2 | +Description: CloudFormation to update DynamoDB with new CIDRs |
| 3 | +Parameters: |
| 4 | + KMSKeyArn: |
| 5 | + Description: 'The arn of the ASEA installer KMS key. Used to access the CIDR Pool DDB table' |
| 6 | + Type: 'String' |
| 7 | +Resources: |
| 8 | + CIDRS3Bucket: |
| 9 | + Type: AWS::S3::Bucket |
| 10 | + DeletionPolicy: Delete |
| 11 | + Properties: |
| 12 | + BucketName: !Sub 'ddb-cidr-${AWS::AccountId}' |
| 13 | + BucketEncryption: |
| 14 | + ServerSideEncryptionConfiguration: |
| 15 | + - ServerSideEncryptionByDefault: |
| 16 | + SSEAlgorithm: 'AES256' |
| 17 | + NotificationConfiguration: |
| 18 | + LambdaConfigurations: |
| 19 | + - Event: 's3:ObjectCreated:*' |
| 20 | + Function: !GetAtt CIDRLambda.Arn |
| 21 | + CIDRBucketInvoke: |
| 22 | + Type: AWS::Lambda::Permission |
| 23 | + Properties: |
| 24 | + Action: 'lambda:InvokeFunction' |
| 25 | + FunctionName: !GetAtt CIDRLambda.Arn |
| 26 | + Principal: 's3.amazonaws.com' |
| 27 | + SourceAccount: !Ref 'AWS::AccountId' |
| 28 | + SourceArn: !Sub 'arn:aws:s3:::ddb-cidr-${AWS::AccountId}' |
| 29 | + CIDRLambda: |
| 30 | + Type: AWS::Lambda::Function |
| 31 | + Properties: |
| 32 | + Code: |
| 33 | + ZipFile: | |
| 34 | + import json |
| 35 | + import boto3 |
| 36 | + import os |
| 37 | + s3_client = boto3.client('s3') |
| 38 | + ddb = boto3.resource('dynamodb') |
| 39 | + ddb_client = boto3.client('dynamodb') |
| 40 | + def handler(event, context): |
| 41 | + # Define variables |
| 42 | + bucket_name = event['Records'][0]['s3']['bucket']['name'] |
| 43 | + file_name = event['Records'][0]['s3']['object']['key'] |
| 44 | + try: |
| 45 | + # Get CIDR allocation file from S3 |
| 46 | + s3_response = s3_client.get_object(Bucket=bucket_name, Key=file_name) |
| 47 | + # Get CIDR list from S3 |
| 48 | + file_content = json.loads(s3_response["Body"].read().decode("utf-8")) |
| 49 | + cidr_list = file_content["cidr-pools"] |
| 50 | + # Get DynamoDB table name (ends in cidr-pool) |
| 51 | + ddb_tables = (ddb_client.list_tables())['TableNames'] |
| 52 | + ddb_table = [table for table in ddb_tables if 'cidr-pool' in table][0] |
| 53 | + # Initialise DynamoDB table resource for the table |
| 54 | + table = ddb.Table(ddb_table) |
| 55 | + # Write CIDRs to the DynamoDB table |
| 56 | + for i, cidr in enumerate(cidr_list): |
| 57 | + cidr['id'] = str(i+1) |
| 58 | + ddb_response = table.put_item( |
| 59 | + Item=cidr |
| 60 | + ) |
| 61 | + print('DynamoDB was successfully updated with the CIDR ranges') |
| 62 | + except Exception as e: |
| 63 | + print(e) |
| 64 | + Description: Lambda to update CIDR ranges in DynamoDB |
| 65 | + Handler: index.handler |
| 66 | + MemorySize: 128 |
| 67 | + Role: !GetAtt CIDRLambdaRole.Arn |
| 68 | + Runtime: python3.9 |
| 69 | + Timeout: 60 |
| 70 | + CIDRLambdaRole: |
| 71 | + Type: 'AWS::IAM::Role' |
| 72 | + Properties: |
| 73 | + AssumeRolePolicyDocument: |
| 74 | + Version: '2012-10-17' |
| 75 | + Statement: |
| 76 | + - Effect: Allow |
| 77 | + Principal: |
| 78 | + Service: 'lambda.amazonaws.com' |
| 79 | + Action: |
| 80 | + - 'sts:AssumeRole' |
| 81 | + Path: '/' |
| 82 | + ManagedPolicyArns: |
| 83 | + - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' |
| 84 | + Policies: |
| 85 | + - PolicyName: RestrictedPermissions |
| 86 | + PolicyDocument: |
| 87 | + Version: 2012-10-17 |
| 88 | + Statement: |
| 89 | + - Sid: Policy0 |
| 90 | + Effect: Allow |
| 91 | + Action: |
| 92 | + - s3:GetObject |
| 93 | + - dynamodb:PutItem |
| 94 | + - kms:Decrypt |
| 95 | + Resource: |
| 96 | + - !Sub 'arn:aws:s3:::ddb-cidr-${AWS::AccountId}/cidr-update-list.json' |
| 97 | + - !Sub 'arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/*' |
| 98 | + - !Ref KMSKeyArn |
| 99 | + - PolicyName: UnrestrictedPermissions |
| 100 | + PolicyDocument: |
| 101 | + Version: 2012-10-17 |
| 102 | + Statement: |
| 103 | + - Sid: Policy1 |
| 104 | + Effect: Allow |
| 105 | + Action: |
| 106 | + - dynamodb:ListTables |
| 107 | + Resource: |
| 108 | + - '*' |
| 109 | +Outputs: |
| 110 | + S3BucketName: |
| 111 | + Description: S3 Bucket for uploading CIDR list |
| 112 | + Value: !Ref CIDRS3Bucket |
0 commit comments