Custom AWS Lambda (with SAM)in AWS Cloud9 — No weirder names

Abhinav Gupta
2 min readMay 3, 2019
Photo by Yancy Min on Unsplash

Problem

Are you facing problems while creating Lambda Function from AWS Cloud9? Like:

  1. No hyphens are allowed in Lambda Name if you create using Cloud9.
  2. If you create Lambda Function using Cloud9, it will assign big weird name like cloud9-xxx-xxx-xxx with custom roles to Lambda functions.

Well, there is a solution to it.

Steps

  1. Create a Cloud9 environment and open IDE. Let's name it, my-dev-env.
  2. In the root folder (my-dev-env), right click and create a new folder. Give your project name. For our understanding, let's name it my-lambda-function.
  3. Under the Lambda project (my-lambda-function), create a file .application.json (with an initial dot)
  4. Write the following content in .application.json
{
"ApplicationName": "mylambdafunction",
"DeploymentMethod": "cloudformation",
"Functions": {
"mylambdafunction": {
"PhysicalId": {
"eu-west-1": "my-lambda-function"
}
}
},
"StackName": "cloud9-my-lambda-function"
}

5. Please note that at some places, I am using mylambdafunction instead of my-lambda-function because hyphens are not allowed at those places, but don’t worry, it will create a Lambda function with hyphens.

6. Create a folder under my-lambda-function. Let’s name it mylambdacode. Write all your code in this folder. For example, in the case of Python lambda_function.py or in case of Node it is index.js. You can also write other code in the same folder. Writing code is out of scope for this article so skipping it.

7. Create another file under my-lambda-function. Let's name it template.yaml

AWSTemplateFormatVersion: '2010–09–09'
Transform: 'AWS::Serverless-2016–10–31'
Description: An AWS Serverless Specification template describing your function.
Resources:
mylambdafunction:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: 'my-lambda-function'
Description: ''
Handler: mylambdacode/lambda_function.lambda_handler
MemorySize: 128
Role: <role-arn>
Runtime: python3.6
Timeout: 15

While copying the above YAML file in Medium, I lost YAML format. So please make sure indentation is correct. Also while copying, hyphens are replaced by dash, please replace all dash by hyphens.

8. Again in the above file at some places, I am using mylambdafunction instead of my-lambda-function because hyphens are not allowed at those places.

You can either pass IAM Role ARN or you can write Cloudformation template for IAM Role and other services in template.yaml to associate with Lambda Function

9. After saving all the above code, you should be able to see my-lambda-function in AWS Resources on the right, and if you expand it you should see mylambdafunction.

10. Right click on mylambdafunction in AWS Resources and choose Deploy.

11. Once deployed, check your Lambda Function in AWS Lambda Console.

--

--