Skip to content

Instantly share code, notes, and snippets.

@ispyinternet
Created February 1, 2018 19:48
Show Gist options
  • Save ispyinternet/97b434a2a58aea5d496ecd87b29e64e9 to your computer and use it in GitHub Desktop.
Save ispyinternet/97b434a2a58aea5d496ecd87b29e64e9 to your computer and use it in GitHub Desktop.
---
AWSTemplateFormatVersion: "2010-09-09"
Description: "Create a Lambda function that will take a comma seperated list of key=value pairs and return an array of key value pairs that can then be used in cloudformation tags resource parameter"
Resources:
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
LambdaTagParser:
Type: AWS::Lambda::Function
Properties:
Handler: index.main
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Runtime: nodejs6.10
Code:
ZipFile: |
exports.main = (event, context, callback) => {
let keyPairString = event.ResourceProperties.data;
let keyPairs = keyPairString.split(',').map( item => ({ Key: item.split('=')[0], Value: item.split('=')[1]}));
sendResponse(event,context,"SUCCESS", { result: keyPairs });
};
function sendResponse(event, context, status, data, err) {
var reason = err ? err.message : '';
var responseBody = {
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
PhysicalResourceId: 'tagparser-' + JSON.stringify(event.ResourceProperties.data),
Status: status,
Data: data
};
console.log("RESPONSE:\n", JSON.stringify(responseBody));
var json = JSON.stringify(responseBody);
var https = require("https");
var url = require("url");
var parsedUrl = url.parse(event.ResponseURL);
var options = {
hostname: parsedUrl.hostname,
port: 443,
path: parsedUrl.path,
method: "PUT",
headers: {
"content-type": "",
"content-length": json.length
}
};
var request = https.request(options, function(response) {
console.log("STATUS: " + response.statusCode);
console.log("HEADERS: " + JSON.stringify(response.headers));
context.done(null, data);
});
request.on("error", function(error) {
console.log("sendResponse Error:\n", error);
context.done(error);
});
request.on("end", function() {
console.log("end");
});
request.write(json);
request.end();
}
Outputs:
TemplateId:
Description: "Template ID"
Value: "LambdaTagParser"
StackName:
Description: "Stack Name"
Value: !Sub "${AWS::StackName}"
FunctionArn:
Description: "Function ARN"
Value: !Ref LambdaTagParser
Export:
Name: !Sub '${AWS::StackName}-FunctionArn'
@ispyinternet
Copy link
Author

use like:

Resources:
  GetResourceTags:
    Type: Custom::GetParameterAsTags
    Version: 1.0
    Properties:
      ServiceToken:
        !Join
        - ":"
        - - "arn:aws:lambda"
          - !Ref AWS::Region
          - !Ref AWS::AccountId
          - "function"
          - !ImportValue "LAMBDA-TAG-PARSER-FunctionArn"
      data: !Ref Tags
 
.
.
.
  Volume:
    Type: AWS::EC2::Volume
    Properties:
      Tags:
        Fn::GetAtt:
        - GetResourceTags
        - result

@steveisoft
Copy link

Is it possible to add additional static tags at the same time use parameterized tags?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment