Forked from ispyinternet/gist:97b434a2a58aea5d496ecd87b29e64e9
Created
May 23, 2018 18:32
-
-
Save jojees/345fcd8cbc315eeea5d94c861eadb023 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment