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:).


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

app.get('/', function(req, res) {
    "Output": "Hello World!"
});'/', function(req, res) {
    "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.


version: 0.1

      - npm install
      - aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.json
  type: zip
    - 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.


 '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.


    "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.

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


 AWSTemplateFormatVersion: 2010-09-09
  - AWS::Serverless-2016-10-31
  - AWS::CodeStar
      Type: String
      Description: AWS CodeStar projectID used to associate new resources to team members
      Type: AWS::Serverless::Function
        Handler: index.handler
        Runtime: nodejs4.3
            !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
            Type: Api
              Path: /
              Method: get
            Type: Api
              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 or tweet me @brian_yamabe. Next time I’ll add support for using TypeScript.

How Do I Document?

I don't have an answer to the question. I'm just riffing here. Trying to figure out how to create value for a yet to be determined audien...… Continue reading

Alexa Skill - Part 4

Published on September 19, 2018

Alexa Skill - Part 3

Published on September 16, 2018