0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AWS】ECS Fargateコンテナの中に入る方法と注意点2点

Posted at

概要

ECS Fargateのコンテナ(タスク)に入る方法およびそれに伴う注意点を紹介します。

注意点

注意点は2点あります。

1点目は、ECSタスクロールに、SSMアクセス関連のポリシーをアタッチすること。ECSコンテナへの接続の際に「SSMセッションマネージャー」を使用するためです。

私が作成したECSのIAM Roleは以下です。ご参考までに。

EcsRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Effect: "Allow"
            Principal:
              Service:
                - "ec2.amazonaws.com"
                - "ecs.amazonaws.com"
                - "ecs-tasks.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      Path: "/"
      RoleName: EcsRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess
        - arn:aws:iam::aws:policy/AmazonECS_FullAccess
        ## - arn:aws:iam::aws:policy/etc...
      Policies:
        - PolicyName: SSMCommunicationsPolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action:
                  - "ssmmessages:CreateControlChannel"
                  - "ssmmessages:CreateDataChannel"
                  - "ssmmessages:OpenControlChannel"
                  - "ssmmessages:OpenDataChannel"
                Resource: "*"

2点目は、--enable-execute-commandフラグをTrueにしておくことです。
以下のCLIコマンドで可能です(CLIの設定がまだの場合は、AWS Cloudshellから実行するのが一番早いと思います)。

$ aws ecs update-service --cluster <クラスター名> --service <サービス名> --enable-execute-command

ただし、現在起動中のECSコンテナには適用されません。
有効になるのは、作り直されたECSからですので、すぐコンテナにアクセスしたい場合は再起動が必要です。

Fargateコンテナの中に入るCLIコマンド

  • ECS -> Service -> Task へ進み、稼働中のコンテナのIDをコピーする
  • AWS Cloudshellへアクセスし、以下のコマンド(ECS Exec)を実行
    • clustercontainerはそれぞれECSクラスタ名やコンテナ名を入れる
    • --taskは上記でコピーしたコンテナIDを貼り付ける
aws ecs execute-command  \
    --region ap-northeast-1 \
    --cluster <クラスター名> \
    --task <コンテナID>\
    --container <コンテナ名> \
    --command "/bin/bash" \
    --interactive

これで実行中のコンテナの中にはいることができました。
以上です!

AWS doc

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?