As a continuation of AWS saam environment, running Lambda with Ruby, I used sam to run WebAPI samples and tests in the local environment with Ruby. This time, I deployed it and ran it on AWS.
Quick Start - AWS Serverless Application Model
From the Package the Application area
Create an S3 bucket for deployment
To deploy, you need to upload the Lambda to the s3 bucket once, instead of just deploying it.
> AWS S3 MB S3://appropriate bucket
make_bucket: a suitable bucket
# Trying to check the bucket in LS
> AWS S3 LS
2019-02-XX XX:XX:XX suitable bucket
I was worried, so I checked the AWS console and saw that the bucket had been created.
Uploading the package to S3
The following command will upload the packaged artifacts (maybe some Lambda code or libraries? zipped together) to s3 and create a local packaged.yaml
> sam package\frzero
--output-template-file packaged.yaml
--s3-bucket, any bucket
Uploading to 818f0d274293fec4b3580cc9b3a0c6e0 643563 / 643563.0 (100.00%)
Successfully packaged artifacts and wrote output template to file packaged.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /Users/kon_yu/devlopment/sam_sample/sam-app/packaged.yaml --stack-name <YOUR STACK NAME>
If you look at the contents of packaged.yaml, there is a file of Cloudformation.
There is an IAM role called HelloWorldFunctionRole.
AWSTemplateFormatVersion: '2010-09-09'
Description: 'sam-app
Sample SAM Template for sam-app
Globals:
Timeout: 3
Timeout: 3
Outputs:
Description: API Gateway endpoint URL for Prod stage for Hello World function
Description: API Gateway endpoint URL for Prod stage for Hello World function
Value:
Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/
Description: Hello World Lambda Function ARN
Description: Hello World Lambda Function ARN
Value:
Fn::GetAtt:
- HelloWorldFunction
- Arn.
Description: Implicit IAM Role created for Hello World function
Description: Implicit IAM Role created for Hello World function
Value:
Fn::GetAtt:
- HelloWorldFunctionRole
- Arn.
Resources:
HelloWorldFunction:
Properties:
CodeUri: s3://appropriate bucket/818f0d274293fec4b3580cc9b3a0c6e0
Environment:
Variables:
PARAM1: VALUE
Events:
HelloWorld:
Properties:
Method: get
Path: /hello
Type: Api
Handler: app.lambda_handler
Runtime: ruby2.5
Type: AWS::Serverless::Function
Transform: AWS::Serverless-2016-10-31
Deploy
sam deploy\frzero
--template-file packaged.yaml \fnDroid
--stack-name sam-app\frzero
--capabilities CAPABILITY_IAM\frz
--region ap-northeast-1
Specify the cloudformation template file (packaged.yaml) to be read by --template file
Specify the cloudformation stack (sam-app) by --stack-name.
--capabilities CAPABILITY_IAM will create an IAM role if you add the option CAPABILITY_IAM. This IAM role is specified as the lambda execution role.
-- Specify the Tokyo region in region(ap-northeast-1)
Operation check
Open Cloudformation from the AWS console, select the sam-app stack you just created, and open the "Output" tab at the bottom.
The HelloWorldApi endpoint has a URL stored in the value. If you open this URL in your browser, you can check its operation.
The library specified in Gemfile is uploaded to S3, and the contents of the library are
It looked like the contents of the .aws-sam/build/HelloWorldFunction that was built and generated with sam build.
Archive: 818f0d274293fec4b3580cc9b3a0c6e0
inflating: app.rb
Inflating: Gemfile
inflating: Gemfile.lock
inflating: .bundle/config
inflating: vendor/bundle/ruby/2.5.0/specifications/mime-types-3.2.2.2.gemspec
inflating: vendor/bundle/ruby/2.5.0/specifications/httparty-0.16.3.gemspec
inflating: vendor/bundle/ruby/2.5.0/specifications/multi_xml-0.6.0.gemspec
inflating: vendor/bundle/ruby/2.5.0/cache/mime-types-data-3.2018.0812.gem
inflating: vendor/bundle/ruby/2.5.0/bin/httparty
inflating: vendor/bundle/ruby/2.5.0/gems/httparty-0.16.3/cucumber.yml
inflating: vendor/bundle/ruby/2.5.0/gems/httparty-0.16.3/.rubocop_todo.yml
Check what you're deploying from the AWS console
Open a browser and select the Tokyo region from the AWS console
When you open Lambda in the AWS console
You can see that lambda was created.
Also, URL seems to change when Cloudformation is deleted and deployed again.
For example, something like this.
- 1st deployment: https://hboimeyaht4.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
- Second deployment: https://8y2few5qxf.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
Future issues.
- One more thing to create your own API Gateway endpoint and the Lambda associated with it
- Insert data into DynamoDB, create an endpoint to retrieve data
- I'll try to apply Lambda Layers to it.
- Name resolution issues on deployment.
Reference information
Are you sure you can be happy with Lambda Layers? Consider moving from an outdated deployment method to a deployment method that leverages Lambda Layers | DevelopersIO
How I set up resource policy for API Gateway in AWS SAM | DevelopersIO
Top comments (0)