Output apikey value in cloud formation
I have a cloudformation template that outputs variables. One of the output variable is
ApiGKeyId:
Description: "Api Key Id"
Value: !Ref ApplicationApiGatewayApiKey
This returns the Id of API gateway key and not the actual value. Is there a way to get the value?
Solution 1:
attribute "Value" is not supported according to below thread~
https://github.com/awslabs/serverless-application-model/issues/206
3rd party maintained available attributes at a glance here: https://theburningmonk.com/cloudformation-ref-and-getatt-cheatsheet/
After some research, I felt there is no other way to retrieve the ApiKey's value but using Custom Resource invoking an lambda function. Here is my sample code fyr.
#######################################################
##### Start of Custom functions #####
#######################################################
ValueFunc:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: >
var response = require('cfn-response');
var AWS = require('aws-sdk');
exports.handler = function(event, context) {
var apiKeyID = event.ResourceProperties.ApiKeyID;
var apigateway = new AWS.APIGateway();
var params = {
apiKey: apiKeyID,
includeValue: true
};
apigateway.getApiKey(params, function(err, ApiKeyData) {
if (err) {
console.log(err, err.stack); // an error occurred
var responseData = { "mykey" : "error reading ApiKey" };
response.send(event, context, response.SUCCESS, responseData);
} else {
console.log(ApiKeyData.value); // successful response
var responseData = { "mykey" : ApiKeyData.value };
response.send(event, context, response.SUCCESS, responseData);
}
});
};
Handler: index.handler
Runtime: nodejs8.10
Timeout: 30
Role: !Sub "arn:aws:iam::${AWS::AccountId}:role/${LambdaExecutionRole}"
GetApiKeyValue:
Type: Custom::LambdaCallout
Properties:
ServiceToken: !GetAtt ValueFunc.Arn
ApiKeyID: !Ref ApiKey