How Do I Structure This JSON CloudFormation Template - json

From this article, it shows how to deploy a AWS::Serverless::Function as a 'proxy' to do routing, a/b testing, etc.
The example template is in YAML, but we need to use JSON as that is what the AWS dotnet templates are built on.
There is a line in the YAML that I cannot figure out how to translate to a JSON template:
LambdaFunctionARN: !Ref LambdaEdgeFunctionSample.Version
In a JSON template a Ref just refers to another resource or parameter. You cannot Ref to a Resource.Property. For that you use Fn::GetAtt.
I have tried Fn::GetAtt, but that errors with "Version is an unknown attribute for this resource" (in the editor) and Template error: every Fn::GetAtt object requires two non-empty parameters, the resource name and the resource attribute during deployment:
"LambdaFunctionARN":{"Fn::GetAtt":["LambdaEdgeFunctionSample","Version"]}}
Mind you, these are both using AWS::Serverless::Function underneath, so there's more transforming going on.
Specifically, when the JSON template is transformed, it does create:
"LambdaEdgeFunctionSampleVersion1cfc342538": {
"Type": "AWS::Lambda::Version",
"DeletionPolicy": "Retain",
"Properties": {
"FunctionName": {
"Ref": "LambdaEdgeFunctionSample"
}
}
},
But I cannot use LambdaEdgeFunctionSampleVersion1cfc342538 as clearly that is a transient id.
How can I accomplish the same as the YAML template in a JSON template?

Apparently, AWS SAM does a transformation on !Ref LambdaEdgeFunctionSample.Version before the template is actually processed.
So, in Json, it's the same and SAM does the transformation:
{"Ref":"LambdaEdgeFunctionSample.Version"}
transforms to
{
"Ref": "LambdaEdgeFunctionSampleVersionf76b18e3ba"
}

Related

How to pass a list as an environment variable to an AWS Lambda function from a JSON config file?

I have a JSON file that is going to contain a number of different lists per client that I am deploying for. These lists are going to serve as container overrides for an ECS task that my Lambda function will be invoking. The JSON config file would look something like this:
{
"clientName": {
"environment": [
{
"name": "name1",
"value": "value1"
}
]
}
}
And my serverless.yml would look something like this:
environment:
CONTAINER_ENVIRONMENT: ${file(serverlessConfig.json):${env:CLIENT_NAME}.environment}
Which results in the following error:
Could not resolve "CONTAINER_ENVIRONMENT" environment variable: Unsupported environment variable format:
[
{
"name": "name1",
"value": "value1"
}
]
I've tried using CloudFormation intrinsic functions such as Fn::Join and Fn::ToJsonString. These both threw an error when trying to run locally using sls invoke local (the errors were the same as the above). After some digging it seems that these functions aren't compatible with the serverless environment property.
The only thing that has worked so far is storing the list as a string in a .env file, but that's not really ideal since these configs could take a number of different environment objects.
Is there any way to get this to work with the setup that I have going?

AWS CloudFormation: "Parameter [subnetIds] is invalid"

I have a AWS CodePipeline to deploy a stack in CloudFormation using a YAML template as well as a template configuration JSON file.
Relevant Template Snippet:
AWSTemplateFormatVersion: '2010-09-09'
...
Parameters:
subnetIds:
Type: List<AWS::EC2::Subnet::Id>
...
Relevant Configuration File Snippet:
{
"Parameters": {
...
"subnetIds": [
"subnet-a",
"subnet-b",
"subnet-c"
]
},
...
}
For some reason the Deploy stage (CloudFormation) keeps failing with Parameter [subnetIds] is invalid, so my question is how do I pass a list of subnetIds to the template from the configuration file?
It is explained here in the docs about list data types, such as:
List<AWS::EC2::Subnet::Id>
An array of subnet IDs, such as subnet-123a351e, subnet-456b351e.
That is to say, all List types in CloudFormation are also comma-separated strings.
Since you are using a CodePipeline Template Configuration File, you will have something like:
{
"Parameters": {
"subnetIds": "subnet-a,subnet-b,subnet-c"
}
}

Is there an alternative to "type": "undefined" in JSON?

I'm working with Amazon API Gateway. I am creating a model for an REST API. The model gets hung up on:
"tiers": {
"type": "array",
"items": {
"type": "undefined"
}
}
The API data model uses JSON schema draft 4.
The error returned is:
Invalid model specified: Validation Result: warnings : [], errors :
[Invalid model schema specified]
Anyone run into this before?
Things I've tried:
Removing this property = script creates model
Changing "Undefined" to "null" = script creates model
The "null" seems like the right option but, I've not been able to back it up. Some guidance and/or clarification would be greatly appreciated.
Thanks,
Todd
You don't seem to be actually defining a schema for your data, refer to the API gateway docs to re-define your model.
undefined is not a valid json value, even though it is valid in javascript. From the official json standard (ECMA-404, Section 5):
A JSON value can be an object, array, number, string, true, false, or
null.
For json, use null instead of undefined: { "something": null }
Using null instead of undefined is definitely not ideal, but it's a standard you can count on when consuming third-party services.

How to generate a Swagger #definition from sample JSON

Take the following #definition from the pet store example. Given a #definition section a JSON structure can be generated
e.g.
Is there something that can do the reverse given a largeish complex JSON file?
Given the below JSON Structure can I get the #defintion section of a swagger file generated to save some typing
{
"variable": "sample",
"object1": {
"obj-field1": "field 1 of object",
"obj-field2": "field 2 of object",
"anArray": [
"Value 1",
{
"anArrayObj1": "obj1fieldinarray",
"anArrayObj2": "obj2fieldinarray"
}
]
}
}
You can use this JSON-to-OpenAPI schema converter:
https://roger13.github.io/SwagDefGen/
(GitHub project)
I haven't used it personally though, so I'm not sure how good it is.
Since OpenAPI uses a subset of JSON Schema, you could also use one of the JSON Schema generators, however you may need to manually tweak the generated definition to make it OpenAPI-compatible.
1 - Paste a response in http://www.mocky.io and get a link to your response
2 - Go to https://inspector.swagger.io/ and make a call to your example response
3 - Select the call from "History" and click "Create API definition"
4 - The swagger definition will be available at https://app.swaggerhub.com/
You can use mock-to-openapi cli tool that generates OpenAPI YAML files from JSON mock.
npm install --global mock-to-openapi
then run the conversion of all *.json files from the folder:
mock-to-openapi ./folder/*.json`
Let's have, for example, json object with:
{
"title": "This is title",
"author": "Roman Ožana",
"content" : "This is just an example",
"date": "2020-05-12T23:50:21.817Z"
}
Tool mock-to-openapi converts JSON to the OpenAPI specification as follows:
type: object
properties:
title:
type: string
example: This is title
author:
type: string
example: Roman Ožana
content:
type: string
example: This is just an example
date:
type: string
format: date-time
example: 2020-05-12T23:50:21.817Z
This works for me:
Generate Swagger REST-client code (and POJO) from sample JSON:
Go to apistudio.io:
Insert -> New Model.
CutNpaste your JSON.
[The Swagger YML file will be generated]
Download -> YAML.
Go to editor.swagger.io:
CutNpaste the YML saved from last step.
Generate Client -> jaxrs-cxf-client (there are many other options).

Building json path expression - presence of DOT in attribute name

We are working with a legacy system which gives json responses. We trying to test these json endpoints with jmeter. So, we are trying to use the json path extractor plugin for the purpose. But the structure of the json path is causing an issue in creating json path expressions.
The structure of the json which we are receiving from the server is as follows.
{
"ns9.Shopping": {
"transactionID": "XXXXXNEKIHJO7SRHN1",
"transactionStatus": "Success",
"ns9.shoppingResponseIDs": {
"ns9.owner": "1P",
"ns9.responseId": "abcdefghijklmnop"
},
"ns9.offersGroup": {"ns9.thanksGiving": [
{
"ns9.owner": "DL",
"ns9.retailOffer": [
{
"ns9.offerId": "offer1DL",
"ns9.price": 2639.08,
"ns9.currencyCode": "USD",
"ns9.taxTotal": 961.08,
"ns9.taxCode": "USD",
.........
The presence of . [DOT] in the attribute name is causing issues in my json path expression.
In short, can some one help in finding the "transactionID" from "ns9.Shopping"?
You can try to add Regular Expression Extractor within your HTTP Request element.
Put this regex:
"transactionID": "([a-zA-Z0-9]*)"
I hope this will help you.