LoginSignup
0
0

More than 3 years have passed since last update.

(備忘録)EC2インスタンスからのAWS CLIコマンド操作

Last updated at Posted at 2020-05-05

1. はじめに

2020年5月5日にヤマムギ様主催にて開催されたヤマムギ vol.9 (AWS)EC2からAWS CLIコマンドを実行してみようハンズオンについて、自分への備忘録としてまとめてみました。ご参考になれば幸いです。

2. 学んだ内容

  • 検証環境の確認
  • AWS CLIでのデフォルトリージョン設定
  • AWS CLIからS3操作
  • AWS CLIからEC2作成&起動

3. 学習サイト

4. 学習メモ

AWS-CLI
// デフォルトリージョンの設定(東京リージョンの場合)
// output formatは、json, text, tableが設定可能
$ aws configure
AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]: ap-northeast-1
Default output format [None]: json

// .configファイルの確認
$ cd
$ cat .aws/config
[default]
output = json
region = ap-northeast-1

// S3新規バケットの作成(AccessDeniedとなる場合はIAMロールにポリシー("Action": "s3:CreateBucket")が足りていない)
// BACKETNAMEの部分は世界で一意(ユニーク)になるように書き換えること
$ aws s3 mb s3://BACKETNAME
make_bucket: BACKETNAME

// S3バケット一覧の表示(AccessDeniedとなる場合はIAMロールにポリシー("Action": "s3:ListAllMyBuckets")が足りていない)
$ aws s3 ls

// EC2上のファイルをS3バケットにオブジェクトをアップロード
$ echo Hello > hello.html
$ aws s3 cp hello.html s3://BACKETNAME/hello.html
upload: ./hello.html to s3://BACKETNAME/hello.html

// S3アクセスコントロールリストの確認
$ aws s3api get-object-acl --bucket BACKETNAME --key hello.html
{
    "Owner": {
        "DisplayName": "accountname",
        "ID": "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
    },
    "Grants": [
        {
            "Grantee": {
                "Type": "CanonicalUser",
                "DisplayName": "accountname",
                "ID": "xxxxxxxxxxxxxxxxx"
            },
            "Permission": "FULL_CONTROL"
        }
    ]
}

// S3アクセスコントロールリストの設定
$ aws s3api put-object-acl --acl public-read --bucket BACKETNAME --key hello.html

// S3アクセスコントロールリストの確認
$ aws s3api get-object-acl --bucket BACKETNAME --key hello.html
{
    ~省略~
        {
            "Grantee": {
                "Type": "Group",
                "URI": "http://acs.amazonaws.com/groups/global/AllUsers"
            },
            "Permission": "READ"
        }
    ]
}

// S3オブジェクトの一覧表示
$ aws s3 ls s3://BACKETNAME

// S3オブジェクトの削除
$ aws s3 rm s3://BACKETNAME/hello.html
delete: s3://BACKETNAME/hello.html

// S3バケットの削除
$ aws s3 rb s3://BACKETNAME
remove_bucket: BACKETNAME

// EC2インスタンスの作成&起動(image-idはリージョン毎にことなるため、リージョン毎のAMIを要確認)
// この例では、東京リージョンが.configに設定されている前提で、東京リージョンのAMIをimage-id指定している
$ aws ec2 run-instances --image-id ami-0f310fced6141e627 --instance-type t2.micro

// EC2インスタンスの確認
$ aws ec2 describe-instances
$ aws ec2 describe-instances --output=table --query 'Reservations[].Instances[].{InstanceId: InstanceId, PrivateIp: join(`, `, NetworkInterfaces[].PrivateIpAddress), GlobalIP: join(`, `, NetworkInterfaces[].Association.PublicIp), Platform:Platform, State: State.Name, SecurityGroupId: join(`, `, SecurityGroups[].GroupId) ,Name: Tags[?Key==`Name`].Value|[0]}'

// EC2インスタンスを終了&削除(インスタンスID xxxxxxxxxxxxxxx は、EC2インスタンスのページから別途個別に調べておく必要あり。)
$ aws ec2 terminate-instances --instance-ids xxxxxxxxxxxxxxx
S3用のIAMロールポリシー
// BACKETNAMEの部分は、当該バケット名に書き換える必要あり

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets",
                "s3:CreateBucket"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObjectAcl",
                "s3:GetObject",
                "s3:ListBucket",
                "s3:DeleteObject",
                "s3:DeleteBucket",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::BACKETNAME",
                "arn:aws:s3:::BACKETNAME/*"
            ]
        }
    ]
}
EC2用のIAMロールポリシー
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeInstances",
                "ec2:TerminateInstances",
                "ec2:RunInstances"
            ],
            "Resource": "*"
        }
    ]
  • 東京リージョンの場合のAmazonマシーンイメージ(AMI)に対するid名(image-id)

image.png

5. ハンズオンで得た豆知識

  • S3バケット名は、世界で一意(ユニーク、重複不可)となるよう命名する必要あり。
  • aws s3 lsコマンドがエラーとなる場合は、IAMロールポリシーのActionキーに"s3:ListAllMyBuckets"の値が設定されていないことが原因。
  • Amazonマシーンイメージ(AMI)のid名(image-id)は、リージョン毎に異なる。

6. 参考サイト

7. おわりに

AWS学習の参考になれば幸いです。
ハンズオン開催してくださいましたヤマムギ様、感謝いたします。

2020/05/05 NISHIZONO Takahiro

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