1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ServerlessでサクッとEC2インスタンスを作成する

Last updated at Posted at 2021-11-22

環境

> node -v
v14.17.4

> yarn -v
1.22.17

環境構築

プロジェクトの作成とTypescriptの追加

yarn init -y
yarn add typescript ts-node @types/node

tsconfig.jsonを追加

{
  "compilerOptions": {
    "target": "esNext",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist", 
    "esModuleInterop": true,  
    "forceConsistentCasingInFileNames": true,
    "strict": true, 
    "skipLibCheck": true
  },
  "exclude": [
     "dist",
     "node_modules"
  ]
}

serverlessフレームワークの導入

yarn add serverless @types/serverless serverless-deployment-bucket

GithubにAWSのアクセスキーとシークレットを保存

image.png

serverless.tsの作成

serverless.ts
import { Serverless } from 'serverless/aws';

export const service: Serverless = {
    service: 'learn-serverless',
    frameworkVersion: '2',
    plugins: [
        'serverless-deployment-bucket'
    ],
    provider: {
        name: 'aws',
        runtime: 'nodejs14.x',
        region: 'ap-northeast-1',
        deploymentBucket: {
            name: 'learn-serverless-preparation',
        },
        timeout: 30,
        memorySize: 1024
    },
    functions: {
    },
    resources: {
        Resources: {
            "EC2Instance": {
                Type: 'AWS::EC2::Instance',
                Properties: {
                    ImageId: 'ami-00f045aed21a55240',
                    InstanceType: 't2.micro',
                    Tags: [
                        {
                            Key: 'Name',
                            Value: 'learn serverless',
                        }
                    ],
                },
            }
        }
    }
}

module.exports = service;

Github actions ワークフローの作成

deploy.yml
on:
  push:
    branches:
      - main

name: Serverless Deploy

jobs:
  deploy:
    name: Serverless Deploy
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@master

      - name: Setup Node.js
        uses: actions/setup-node@v1
        with:
          node-version: '14.x'

      - name: Install Dependencies
        run: |
          yarn install

      - name: Deploy to aws
        run: yarn serverless deploy
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

EC2インスタンスの確認

無事に作成できました。
image.png

参考URL

ローカルに TypeScript の実行環境をサクッと構築する手順
CloudFormation入門 #1 「EC2インスタンスの作成」
GitHub Actionsを使ってAWS Lambdaへ自動デプロイ (詳説+デモ手順付きver)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?