Create custom infrastructure and reuse it on multiple projects

In this tutorial we will go through the process of creating a custom infrastructure component in Microtica, sharing and reusing it on different projects.

The component we’ll create will expose APIs for translation, and we’ll use a combination of 3 AWS services:

  • Lambda
  • API Gateway
  • Amazon Translate

This component is available on Microtica’s GitHub account.

Below are the code snippets used in the video tutorial:

sudo npm install -g yo
sudo npm install -g @microtica/generator-microtica-component
yo

index.json

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Resources": {
        "Lambda": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "Handler": "index.handler",
                "Runtime": "nodejs12.x",
                "InlineCode": "const { Translate } = require(\"aws-sdk\");\n\nexports.handler = async function(event, context) {\n    console.log(\"EVENT\", JSON.stringify(event));\n\n    const body = JSON.parse(event.body);\n\n    const { TranslatedText: translatedText } = await new Translate({ region: \"eu-central-1\" }).translateText({\n        SourceLanguageCode: body.sourceLanguage,\n        TargetLanguageCode: body.targetLanguage,\n        Text: body.text\n    }).promise();\n\n    const response = {\n        statusCode: 200,\n        body:JSON.stringify({translatedText})\n    };\n    return response;\n}\n",
                "Policies": [
                    "AWSLambdaExecute",
                    "TranslateFullAccess"
                ],
                "Events": {
                    "ApiEvent": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/translation",
                            "Method": "post",
                            "RestApiId": {
                                "Ref": "ApiGatewayApi"
                            }
                        }
                    }
                }
            }
        },
        "ApiGatewayApi": {
            "Type": "AWS::Serverless::Api",
            "Properties": {
                "StageName": "prod"
            }
        }
    },
    "Outputs": {
        "ApiUrl": {
            "Value": {
                "Fn::Sub": "https://${ApiGatewayApi}.execute-api.${AWS::Region}.amazonaws.com/prod"
            }
        }
    }
}

Lambda function code:

const { Translate } = require("aws-sdk");
exports.handler = async function (event, context) {
    const body = JSON.parse(event.body);

    const {
        TranslatedText: translatedText
    } = await new Translate({ region: "eu-central-1" }).translateText({
        SourceLanguageCode: body.sourceLanguage,
        TargetLanguageCode: body.targetLanguage,
        Text: body.text
    }).promise();

    const response = {
        statusCode: 200,
        body: JSON.stringify({ translatedText })
    };

    return response;
}