This is an open-source solution to deploy AutoReplication of Parameter Store Entries using CloudTrail
to route the deployment event through Cloudwatch Events
, and EventBrigdge
, across regions if it is the case to an endpoint
a lambda function
to replicate parameter entries at the moment of creation or thru a scheduled event in CloudWatch which rewrites the values if applicable.
Hence an Entry created in a central parameter store, say in US-EAST-1, it is replicated in US-EAST-2. If it is already in existence in US-EAST-2 then it is updated.
- Intermedial to advance level in Python. So, you can adapt and customized the
auto-replicate-parameter-store.py
files to your need an use cases. - Basic to intermedial level in json to edit json rules in
EventBridge Rules
to modify it if needed to your use case, since we give granular limited access to AWS resources. - One AWS Region known as the "the Master Region"* to deploy the entries at the Parameter Store to be replicated.
- In the Master Region you choose for the Parameter Store Entries you will need the following
- An
Eventbridge
rule in the master region in order to pass the "PutParameter" events fromcloudtrail
to the lambda function as an endpoint. - An
Eventbridge
rule in the master region in order to schedule a scan on entries in the parameter store that are to be replicated and whose values may have changed. - A
Lambda Function
called auto-replicate-parameter-store as an endpoint to the rules created inEventbridge
.
- An
- IAM
- Lambda
- CloudWatch
- CloudTrail
- SSM Parameter Store
- Key Management Service (KMS)
- Python 3.9
In this case Identity and Access Management (IAM)
is a global element, so do not worry about what region you are in at the moment of logging in. However, though some AWS Services are global like this one and S3
, some others like EventBridge
, CloudWatch
, and Lambda
are regional. Therefore, be sure you are in us-east-1 (N. Virginia) for most of the purposes of this project.
We need one role auto-replicate-parameter-store-role with limited granular permissions to interact with other AWS Services such as: KMS, EC2, SSM, and CloudWatch Logs for this project
Create and Attach the following policy to the role auto-replicate-parameter-store-role
policy.json - IAM Policy to authorize auto-replicate-parameter-store-role to replicate Parameter Store Entries
See policy.json
or copy paste from here...
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ssm:DescribeParameters",
"ec2:DescribeRegions"
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"ssm:PutParameter",
"kms:Encrypt",
"ssm:ListTagsForResource",
"kms:ReEncryptTo",
"ssm:GetParametersByPath",
"ssm:GetParameters",
"kms:GenerateDataKeyPair",
"ssm:GetParameter"
],
"Resource": [
"arn:aws:kms:*:111111111111:key/75fcc799-de1b-42a9-9a12-a23b31111111111",
"arn:aws:ssm:*:111111111111:parameter/*"
]
}
]
}
EventRule.json - This rule filters create or launch events coming from AWS API Call via CloudTrail
that start with the event name "PutParameter" which is the one create Paramter Store entries.
See EventRule.json
or copy paste from here...
{
"source": [
"aws.ssm"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"ssm.amazonaws.com"
],
"eventName": [
"PutParameter"
]
}
}
Note: sometimes when creating complex custom rules such as when using prefix feature it is necessary to create them in EventBridge or to update the very same rules. If it is done CloudWatch directly it may not not work. Hence, we best configure the rules in EventBridge even though the end result is also shown in CloudWatch.
auto-replicate-parameter-store - Lambda function that we deploy in the Designated Region or in our case the us-east-1 region to replicate . It is triggered by a "PutParameter" coming from CloudWatch.
We wil use the Default aws/ssm arn key to secure our project further
A Parameter Store Entry is deployed either by using the console or the AWS SDK for Python (Boto3). Yet, all replication is going to be done by the auto-replicate-parameter-store lambda function in us-east-1. The New Parameter Store Entry with the tag replicate/yes or replicate/us-east-2 generates an event metadata; the timestamp, who was the creator, ARN of the creator, etc, but the one we really need is the tag containing the key/value pair = replicate/yes.
Then, CloudWatch
in us-east-1 filters the creation event based on EventRule.json This rule looks for any event that has "PutParameter", it matches the event and sends the metadata to the lambda funcion auto-replicate-parameter-store as an endpoint.
Thus, the lambda function checks if the value of replicate tag is set to yes. If true auto-replicate-parameter-store lambda funtion is fired and proceeds to replicate the entry into whatever regions are set in the Environment variables in the Lambda function.
In Summary, The purpose of this pipeline is to centralize the control Parameter Store Entries from a centralized Parameter Store in a designated Region. In this way is easier to manage and monitor entries that are to be used across regions it the Parameter Store
Log in to Account ID 111111111111. This is the account number we are going to use as reference in this exercise. When rewriting policies or Rules remember to replace 111111111111 with your account number.
- At the console screen go to services and type in the text box
"KMS"
or under AllServices > Security, Identity, & Compliance > Key Management Service (KMS)
- In
Key Management Service
(KMS) menu > go toAWS managed keys
and click"aws/ssm"
Under AWS managed keys list - In General Configuration copy and save the Key ARN. For the purposes of this example the Key ID is "arn:aws:kms:us-east-1:111111111111:key/75fcc799-de1b-42c7-9a12-a23b31111111111"
2.1 Create a policy.json IAM policy to allow auto-replicate-parameter-store-role to have all required autorizations to replicate Parameter Store Entries:
- At the console screen go to services and type in the text box
"IAM"
or under AllServices > Security, Identity, & Compliance > IAM
- In
Identity and Access Managment (IAM) menu > go to Policies
and click"Create policy"
button - Click Create policy next.
- In Create policy window select JSON tab. Click and copy-paste the following policy and click the "Next: tags" button:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ssm:DescribeParameters",
"ec2:DescribeRegions"
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"ssm:PutParameter",
"kms:Encrypt",
"ssm:ListTagsForResource",
"kms:ReEncryptTo",
"ssm:GetParametersByPath",
"ssm:GetParameters",
"kms:GenerateDataKeyPair",
"ssm:GetParameter"
],
"Resource": [
"arn:aws:kms:*:111111111111:key/75fcc799-de1b-42c7-9a12-a23b31111111111",
"arn:aws:ssm:*:111111111111:parameter/*"
]
}
]
}
- Click "Next: Review" button
- In Review policy window in Name type "policy.json"
- In Description type "Rule to enable auto-replicate-parameter-store-role Rule to replicate parameter store entries" and click "Create policy".
Note Under Resource replace the KMS Key arn and the Account number wherever applicable
"Resource": [
"arn:aws:kms:*:111111111111:key/75fcc799-de1b-42c7-9a12-a23b31111111111",
"arn:aws:ssm:*:111111111111:parameter/*"
]
- Let us go to IAM
- At the console screen go to services and type in the text box
"IAM"
or under All services > Security, Identity, & Compliance > IAM - In Create Role window > Under "Select type of trusted entity" keep AWS service as your choice
- In "Choose a use case" select "Lambda" and click "Next: Permissions" button
- In next window, under Attach Permissions policies click Filter policies and checkmark "Customer managed"
- Scroll down and checkmark the Customer managed policy "policy.json"
- Click "Next:Tags" button and click "Next: Review" button too
- Under Review, in
Role name *
type "auto-replicate-parameter-store-role" - In
Role description
type "Resource Role to replicate parameter store entries" Observe that in Trusted entities you got AWS service: lambda.amazonaws.com and the recently created policy attached to the role - Click "Create Role Button"
We deploy our lambda function in Virginia Region/us-east-1. This is the endpoint for any new entry in the parameter store in us-east-1 that is to be replicated to any other region that is configured in the pipeline.
- First, be sure you are in us-east-1 (It seems repetive, but it is easy to be in the wrong region and fail to do the pipeline configuration) . In the console click the services tab and look for Lamdba under
All services > Compute > Lambda or just type lambda in the text box. then hit Lambda
- In the AWS lambda window go to Functions.
- Click the "Create function" buttom.
- You will the following options to create your function:
Author from scratch
,Use blueprint
,Container Image
, andBrowse serverless app repository
. ChooseAuthor from scratch
. - In Function name type "auto-replicate-parameter-store" or any name you choose to and in Runtime look for Python 3.9
- In Permissions - click
Change default execution role
and select "Use an existing role". In the dialog box that opens up look for "auto-replicate-parameter-store-role", this is the role we created in the previous step. - Click "Create function" button
- Under Code source > In Environment click
lambda_function.py
- Delete all existing code an replace it with the code provided in the
auto-replicate-parameter-store.py
file - Once you paste the new code click "Deploy"
- Click the
Configuration
tab an go toEnvironment variables
. - In
Environment variables
clickEdit
Button and a newEdit environment variables
window will open. - Click
Add environment variable
Next underKey
type region, and underValue
type us-west-2,us-east-2 (Notice that when adding different regions we use comma to separate the values and no spaces). ClickSave
Voila your lambda function is set!
4. In EventBridge in us-east-1 create a rule and use as target to replicate entries at moment of creation.
Create a rule that captures all the parameter store entry creation events in us-east-1
using AWS API Call via CloudTrail
and select auto-replicate-parameter-store as target in order to automatically replicate the entry at the moment of deployment to whatever regions were set in the Environment variables
at the lambda function:
- Be sure you are in
us-east-1
region - At the console screen go to services and type in the text box
"EventBridge"
or underAll services > Application Integration > Amazon EventBridge
- In the Amazon EventBridge menu select Rules and click "Create Rule" button
- Under Name and Description > Name type "EventPutParameter"
- Add a Description "Rule to send parameter store entry creation events to auto-replicate-parameter-store lambda function" if you choose to, it is optional
- In Define pattern choose
"Event pattern" > Custom Pattern
- Copy paste the following json in Event Pattern Text Box
{
"source": [
"aws.ssm"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"ssm.amazonaws.com"
],
"eventName": [
"PutParameter"
]
}
}
... Click "Save"
Notice that "PutParameter" is the Event Name that is generated when a new entry is created at the parameter store
- In
Select event bus
section leave it as it isSelect an event bus`>`AWS default event bus
- In Select
Targets > in Target click the text box, scroll up and select "Lambda Function"
- In Topic text box select "auto-replicate-parameter-store"
- Click
"Create Rule"
button.
5. Configure a Schedule Rule that updates Parameter Store Entries set to be replicated across regions based on a fixed rate
- Be sure you are in
us-east-1
region - At the console screen go to services and type in the text box
"EventBridge"
or underAll services > Application Integration > Amazon EventBridge
- In the Amazon EventBridge menu select Rules and click "Create Rule" button
- Under Name and Description > Name type "replicate-parameters-by-schedule"
- Add a Description "Rule to trigger the replication of Paramater Store Entries set to replicate/yes across regions" if you choose to, it is optional
- In Define pattern choose
" > Schedule"
- In
Fixed rate every
Type24
and selectHours
(Or just choose any time schedule you want) - In
Select event bus
section leave it as it isSelect an event bus`>`AWS default event bus
- In Select
Targets > in Target click the text box, scroll up and select "Lambda Function"
- In Topic text box select "auto-replicate-parameter-store"
- Click
"Create Rule"
button.
Note: The Fixed Rate you set determines how many times the parameter store will be updated per day
Let us create a new entry at the Parameter Store set to be replicated:
- Be sure you are in
us-east-1
region - At the console screen go to services and type in the text box
"Parameter Store"
or underAll services > Management & Governance > Systems Manager
- In the
AWS Systems Manager
menu selectParameter Store
and clickCreate Parameter
button - In
Create parameter > Parameter details > Name
typetest-parameter
- In
Create parameter > Parameter details > Description — Optional
typeParameter to be replicated in Ohio and Oregon
- In
Create parameter > Parameter details > Tier
leaveStandard
- In
Create parameter > Parameter details > Type
chooseSecureString
(in order to test encryption/decryption capabilities) - In
Create parameter > Parameter details > KMS key source
leaveMy current account
- In
Create parameter > Parameter details > KMS Key ID
leavealias/aws/ssm
- In
Create parameter > Parameter details > Value
typeHey it is replicated!
11 InTags
clickAdd tag
- In
Key
text box typereplicate
, and inValue
text box typeyes
and clickCreate parameter
- Now, You will see the same entry replicated at the Parameter Store in us-east-2 and us-west-2
Note: To implement the replication in other regions, repeat modify the "Environment Variable > region" of the lambda function as applicable in your project