Skip to content

Commit 1a64f6e

Browse files
khawsMuhammad KhasBrian969hickeydh-aws
authored
Added in CFN for populating the DDB (#919)
* Added in CFN for populating the DDB * Added in permissions for KMS Decryption * Move folders * Added KMS Key Parameter Co-authored-by: Muhammad Khas <mxk@amazon.com> Co-authored-by: Brian969 <56414362+Brian969@users.noreply.github.com> Co-authored-by: hickeydh-aws <hickeydh@amazon.com>
1 parent b56b541 commit 1a64f6e

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## CloudFormation template for DynamoDB table
2+
3+
This CloudFormation script does the following:
4+
5+
1. Create an Amazon S3 bucket for the JSON file containing CIDR ranges
6+
1. Create an AWS Lambda function that will populate the DynamoDB table
7+
1. Setup a trigger such that the Lambda function will run everytime a file is uploaded in the bucket
8+
9+
Usage:
10+
11+
1. Run the CloudFormation template in the "home" region
12+
1. Navigate to Outputs and note the Amazon S3 bucket's name
13+
1. Modify the attached sample JSON file, as appropriate
14+
1. Upload the JSON file in the S3 bucket
15+
1. The DynamoDB table will be populated with the CIDR ranges
16+
17+
Precautions:
18+
19+
1. The JSON file must follow the pattern in the sample file but any number of CIDRs can be added
20+
1. Uploading a second file will overwrite the DynamoDB with the new values
21+
1. The file must have a name of `cidr-update-list.json` for the IAM permissions to work correctly
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"cidr-pools": [
3+
{
4+
"cidr": "10.0.0.0/18",
5+
"pool": "prod_eu-west-2",
6+
"description": "Production supernet for region eu-west-2",
7+
"region": "eu-west-2"
8+
},
9+
{
10+
"cidr": "10.0.64.0/16",
11+
"pool": "dev_eu-west-2",
12+
"description": "Development supernet for region eu-west-2",
13+
"region": "eu-west-2"
14+
}
15+
]
16+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)