AWS CodeStar and TypeScript - Part 2

Reading time ~2 minutes

In this post I’ll review the files that CodeStar creates for a default project. The process of starting a project is fairly simple, but if you want a walkthrough you can go to the first post in this series.

If my understanding is deficient in any area below, please let me know so I can update this post.

Source Code & Infrastructure

When the project is created, not only is the source code created, it is also put into CodeCommit for source control. Additional CodeBuild and CodeDeploy projects are created to do the build and deploy respectively (they did a good job of naming :+1:).

app.js

var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.send({
    "Output": "Hello World!"
  });
});

app.post('/', function(req, res) {
  res.send({
    "Output": "Hello World!"
  });
});


// Export your Express configuration so that it can be consumed by the Lambda handler
module.exports = app

This code sets up the Express app and sets it to respond to GET and POST requests.

buildspec.yml

version: 0.1

phases:
  build:
    commands:
      - npm install
      - aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.json
artifacts:
  type: zip
  files:
    - template-export.json

The buildspec.yml file is used by CodeBuild to package the local environment into the deployable artifact. My understanding is that the source code from CodeCommit is copied into the environment, then any commands you run add to the environment, and this environment is packaged as an artifact, a .zip file in this case.

In the build phase the ‘npm install’ command is run to setup the npm environment. The ‘aws cloudformation package’ command takes template.yml (described below) and generates a CloudFormation template.

The artifacts phase declares where to find the files included in the output of the build. The ‘files’ declaration says to grab the template that was created with the ‘aws cloudformation package’ command. I’m assuming the type: zip declaration is something special that says to zip the environment because it the source is never explicitly defined.

index.js

 'use strict';
  
  const awsServerlessExpress = require('aws-serverless-express')
  const app = require('./app')
  const server = awsServerlessExpress.createServer(app)
  
  exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);

This code initializes the app that will be referenced by the Lambda function defined in template.yml (described below). It only exports the function handler but the definitions of what is handled happens in template.yml and the handling happens in app.js.

package.json

{
    "name": "express-HelloWorld",
    "description": "Hello World ExpressJS WebService",
    "version": "0.0.1",
    "private": true,
    "dependencies": {
        "express": "latest",
        "aws-serverless-express": "latest"
    }
}

The package.json describes the project and includes the dependencies of the npm application. In this case ‘express’ and ‘aws-serverless-express’ are needed to run the project.

README.md

There is a README file that should be updated to describe the application.

template.yml

 AWSTemplateFormatVersion: 2010-09-09
  Transform:
  - AWS::Serverless-2016-10-31
  - AWS::CodeStar
  
  Parameters:
    ProjectId:
      Type: String
      Description: AWS CodeStar projectID used to associate new resources to team members
  
  Resources:
    HelloWorld:
      Type: AWS::Serverless::Function
      Properties:
        Handler: index.handler
        Runtime: nodejs4.3
        Role:
          Fn::ImportValue:
            !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
        Events:
          GetEvent:
            Type: Api
            Properties:
              Path: /
              Method: get
          PostEvent:
            Type: Api
            Properties:
              Path: /
              Method: post

This is a Serverless Application Model (SAM) template which is template specifically designed to describe Lambda functions so they can be deployed by CloudFormation. It describes the Resource (a Lambda function), the Handler (index.handler from index.js), the Runtime (nodejs4.3), and the Events setup API Gateway to respond to HTTP events (GET and POST in this case).

Those are the defaults which are pretty straightforward. Please email me at brian@yamabe.net or tweet me @brian_yamabe. Next time I’ll add support for using TypeScript.

Perils of a Family-First Software Developer

I've just survived a RIF, my skills and tool set are built around technologies that were modern 10 years ago, my interviewing skills have...… Continue reading

AWS CodeStar and TypeScript - Part 3

Published on May 28, 2017

AWS CodeStar and TypeScript - Part 1

Published on May 16, 2017