Skip to content

Instantly share code, notes, and snippets.

@schlarpc
Created March 22, 2020 07:31
Show Gist options
  • Save schlarpc/f6a91eaac8eab00d7f8b91b1d09bf66f to your computer and use it in GitHub Desktop.
Save schlarpc/f6a91eaac8eab00d7f8b91b1d09bf66f to your computer and use it in GitHub Desktop.
Autoincrementing variable in CloudFormation
{
"Outputs": {
"Value": {
"Value": {
"Ref": "ParameterValue"
}
}
},
"Parameters": {
"ParameterName": {
"Type": "AWS::SSM::Parameter::Name"
},
"ParameterValue": {
"Type": "AWS::SSM::Parameter::Value<String>"
}
},
"Resources": {
"Increment": {
"DependsOn": [
"IncrementLogGroup",
"IncrementPolicy"
],
"Properties": {
"ParameterName": {
"Ref": "ParameterName"
},
"ParameterValue": {
"Ref": "ParameterValue"
},
"ServiceToken": {
"Fn::GetAtt": [
"IncrementFunction",
"Arn"
]
}
},
"Type": "AWS::CloudFormation::CustomResource"
},
"IncrementFunction": {
"Properties": {
"Code": {
"ZipFile": "def handler(event, context):\n import boto3\n import json\n import urllib.parse\n import urllib.request\n\n print(\"Request:\", json.dumps(event))\n\n resource_response = {\n \"Status\": \"SUCCESS\",\n \"PhysicalResourceId\": (\n event[\"ResourceProperties\"].get(\"ParameterName\") or\n event.get(\"PhysicalResourceId\")\n ),\n \"StackId\": event[\"StackId\"],\n \"RequestId\": event[\"RequestId\"],\n \"LogicalResourceId\": event[\"LogicalResourceId\"],\n }\n\n try:\n ssm = boto3.client(\"ssm\")\n if event[\"RequestType\"] in {\"Create\", \"Update\"}:\n next_value = str(int(event[\"ResourceProperties\"][\"ParameterValue\"]) + 1)\n ssm.put_parameter(\n Name=event[\"ResourceProperties\"][\"ParameterName\"],\n Value=next_value,\n Type=\"String\",\n Overwrite=True,\n )\n except Exception as ex:\n resource_response.update({\n \"Status\": \"FAILED\",\n \"Reason\": f\"{ex.__class__.__qualname__}: {ex}\"\n })\n\n print(\"Response:\", json.dumps(resource_response))\n\n response = urllib.request.urlopen(\n urllib.request.Request(\n url=event[\"ResponseURL\"],\n data=json.dumps(resource_response).encode(\"utf-8\"),\n method=\"PUT\",\n )\n )\n"
},
"Handler": "index.handler",
"MemorySize": 512,
"Role": {
"Fn::GetAtt": [
"IncrementRole",
"Arn"
]
},
"Runtime": "python3.7"
},
"Type": "AWS::Lambda::Function"
},
"IncrementLogGroup": {
"Properties": {
"LogGroupName": {
"Fn::Join": [
"/",
[
"/aws/lambda",
{
"Ref": "IncrementFunction"
}
]
]
}
},
"Type": "AWS::Logs::LogGroup"
},
"IncrementPolicy": {
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"ssm:PutParameter"
],
"Effect": "Allow",
"Resource": [
{
"Fn::Join": [
":",
[
"arn",
{
"Ref": "AWS::Partition"
},
"ssm",
{
"Ref": "AWS::Region"
},
{
"Ref": "AWS::AccountId"
},
{
"Fn::Join": [
"/",
[
"parameter",
{
"Ref": "ParameterName"
}
]
]
}
]
]
}
]
},
{
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"IncrementLogGroup",
"Arn"
]
}
]
}
]
},
"PolicyName": "IncrementPolicy",
"Roles": [
{
"Ref": "IncrementRole"
}
]
},
"Type": "AWS::IAM::Policy"
},
"IncrementRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
]
}
},
"Type": "AWS::IAM::Role"
}
}
}
{
"Outputs": {
"Value": {
"Value": {
"Fn::GetAtt": [
"Stack",
"Outputs.Value"
]
}
}
},
"Resources": {
"Parameter": {
"Properties": {
"Tier": "Standard",
"Type": "String",
"Value": "1"
},
"Type": "AWS::SSM::Parameter"
},
"Stack": {
"Properties": {
"Parameters": {
"ParameterName": {
"Ref": "Parameter"
},
"ParameterValue": {
"Ref": "Parameter"
}
},
"TemplateURL": "/path/to/child.json"
},
"Type": "AWS::CloudFormation::Stack"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment