2
0

More than 1 year has passed since last update.

AWS 使い捨て Linux

Last updated at Posted at 2021-09-30

ちょこっと EC2 を作って使い捨てしたい時のメモ。

SSH key pair の作成

鍵を作る

export AWS_PROFILE=exampleprofile
aws ec2 create-key-pair --key-name trash --query 'KeyMaterial' --output text > trash.pem
chmod 400 trash.pem

CloudFormation ファイルを作る

trash.yaml という名前のファイルを作る。

AWSTemplateFormatVersion: "2010-09-09"
Resources:
  Trash:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t3.micro # インスタンスの種類
      ImageId: ami-02892a4ea9bfa2192 # Amazon Linux 2 64-bit x86
      KeyName: trash
      SecurityGroups:
      - Ref: TrashSecurityGroup
      UserData: # 最初からパッケージを更新しておく
        Fn::Base64: !Sub |
          #!/bin/bash -xe
          yum update -y
          
  TrashSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Redmine security
      SecurityGroupIngress:
      - CidrIp: 0.0.0.0/0 # ご自宅の IP。自宅からしか見えないように。
        FromPort: '22' # SSH のために開けるポート
        ToPort: '22'
        IpProtocol: tcp
        
Outputs:
  PublicIP: # 作った IP アドレスを表示
    Value: !GetAtt Trash.PublicIp

CloudFormation で EC2 を作る

aws cloudformation create-stack --stack-name trash --template-body file://trash.yaml

様子見

aws cloudformation describe-stack-events --stack-name trash | jq '.StackEvents[] | .EventId'

終わるまで待つ

aws cloudformation wait stack-create-complete --stack-name trash

しばらく待って出来上がった IP を確認

aws cloudformation describe-stacks --stack-name trash | jq -r '.Stacks[].Outputs[].OutputValue'

返ってきた IP の ec2-user ユーザにログインする。

ssh -i trash.pem ec2-user@13.114.8.76

削除

aws cloudformation delete-stack --stack-name trash
aws ec2 delete-key-pair --key-name trash
rm trash.pem

おまけ Node のインストール

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install 12.13.1
nvm use 12.13.1

2
0
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
2
0