-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-formation.yml
92 lines (82 loc) · 2.7 KB
/
cloud-formation.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
AWSTemplateFormatVersion: "2010-09-09"
Description: "Periodically put a random value into AWS Parameter Store"
Parameters:
Rate:
Description: "A CloudWatch rate expression to schedule the insert"
Type: "String"
Default: "1 minute"
Key:
Description: "The Systems Manager Parameter Store key where the value will be stored (no leading '/')"
Type: "String"
Default: "parameter-rando"
Resources:
# IAM role for Lambda function
LambdaRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
- PolicyName: "allow-put-param"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: ["ssm:PutParameter"]
Resource: [!Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${Key}"]
# Lambda function to put parameter
LambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Code:
ZipFile: !Sub |
from os import environ
from uuid import uuid4
from boto3 import client
def lambda_handler(event, context):
value = str(uuid4())
client('ssm').put_parameter(
Name=environ['PARAM_STORE_KEY'],
Value=value,
Overwrite=True,
Type='String',
Description='random value provided by parameter-rando'
)
return True
Description: "put a random value to AWS Systems Manager Parameter Store"
Handler: "index.lambda_handler"
MemorySize: 128
Role: !GetAtt ["LambdaRole", "Arn"]
Runtime: "python3.6"
Timeout: 10
Environment:
Variables:
PARAM_STORE_KEY: !Ref "Key"
# CloudWatch Events schedule
CloudWatchSchedule:
Type: "AWS::Events::Rule"
Properties:
Description: "Schedule for parameter-rando"
ScheduleExpression: !Sub "rate(${Rate})"
State: "ENABLED"
Targets:
- Arn: !GetAtt ["LambdaFunction", "Arn"]
Id: "parameter-rando-schedule"
# Permissions for CloudWatch to invoke Lambda function
CloudWatchPermissions:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName: !Ref "LambdaFunction"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt ["CloudWatchSchedule", "Arn"]