You can do it right away with Serverless Framework Serverless on AWS (API + Lambda [java] is easy to set up)

Introduction

Setting up the environment

Serverless Framework

Install Serverless Framework

$ node -v
v5.12.0
$ npm install -g serverless
$ serverless —version
1.5.1

AWS CLI setup

$ pip install awscli
$ aws configure
AWS Access Key ID [None]:(Access Key)
AWS Secret Access Key [None]:(Secret Key)
Default region name [None]: ap-northeast-1
Default output format [None]:json

Deploy with Serverless Framework

Creating a Service

$ serverless create --template aws-java-maven --path myService
・
・
Serverless: Successfully generated boilerplate for template: "aws-java-maven"
Serverless: NOTE: Please update the "service" property in serverless.yml with your service name

Import to STS (Eclipse)

Modify serverless.yml + Build

serverless.yml


provider:
  name: aws
  runtime: java8
  stage: prod
  region: ap-northeast-1

Deploy Part 1 (Lambda only)

$ cd myService
$ serverless deploy -v
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3 (1.98 MB)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - aws-java-maven-prod
・
・
Serverless: Stack update finished...
Service Information
service: aws-java-maven
stage: prod
region: ap-northeast-1
api keys:
  None
endpoints:
  None
functions:
  aws-java-maven-prod-hello: arn:XXXXXXXX
$ echo '{"key1":"value1","key2":"value2","key3":"value3"}' > event.json
$ serverless invoke --function hello -p event.json
{
    "statusCode": 200,
    "body": "{\"message\":\"Go Serverless v1.x! Your function executed successfully!\",\"input\":{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}}",
    "headers": {
        "X-Powered-By": "AWS Lambda & serverless"
    },
    "isBase64Encoded": false
}

Deploy Part 2 (API Gateway + Lambda)

serverless.yml


functions:
  hello:
    handler: com.serverless.Handler
    events:
      - http:
          path: users/create
          method: get
$ serverless deploy -v
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3 (1.98 MB)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - aws-java-maven-prod
・
・
Service Information
service: aws-java-maven
stage: prod
region: ap-northeast-1
api keys:
  None
endpoints:
  GET - https://XXXXXXXX.execute-api.ap-northeast-1.amazonaws.com/prod/users/create
functions:
  aws-java-maven-prod-hello: arn:XXXXXXXX
$ curl https://XXXXXXXX.execute-api.ap-northeast-1.amazonaws.com/prod/users/create
{"message":"Go Serverless v1.x! Your function executed successfully!","input":{"resource":"/users/create","path":"/users/create","httpMethod":"GET","headers":{"Accept":"*/*","CloudFront-Forwarded-Proto":"https","CloudFront-Is-Desktop-Viewer":"true","CloudFront-Is-Mobile-Viewer":
・
・

Delete Stack

$ serverless remove
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
.....................
Serverless: Stack removal finished...

Summary

Recommended Posts

You can do it right away with Serverless Framework Serverless on AWS (API + Lambda [java] is easy to set up)
How to deploy Java to AWS Lambda with Serverless Framework
Notes on what to do when EC2 is set up with t2.micro
How to use Java framework with AWS Lambda! ??
Is Java on AWS Lambda slow?
With [AWS] CodeStar, you can build a Spring (Java) project running on Lambda in just 3 minutes! !!
Can you do it? Java EE
[Even beginners can do it! ] How to create Java environment on Windows 10 (JDK14.0.1)
How to use Java API with lambda expression
[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)
I want to hit the API with Rails on multiple docker-composes set up locally
You can do it with copy! Aspect-oriented programming (Android)
Connect to Access database with Java [UCan Access] * Set until it can be executed with VS Code
Serverless Framework template to schedule start / stop EC2 on Lambda
Regularly post imaged tweets on Twitter with AWS Lambda + Java
Create a SlackBot with AWS lambda & API Gateway in Java
When I try to use the AWS SDK with Ruby + Lambda, `sam local` is messed up.