Giter Site home page Giter Site logo

aws-cdk-private-subnet-ec2-and-s3-goofys's Introduction

aws-cdk-private-subnet-ec2-and-s3-goofys

構成図

SSH アクセス

EC2_INSTANCE_ID=$(aws ec2 describe-instances \
    --filters "Name=tag:Name,Values=AwsCdkTemplate-AwsCdkTemplateStack/AwsCdkTemplate-AwsCdkTemplateStack-general_purpose_ec2" \
    --query "Reservations[].Instances[?State.Name=='running'].InstanceId[]" \
    --output text)
ssh -i ~/.ssh/ec2/id_ed25519 admis@$EC2_INSTANCE_ID

S3 との疎通確認

  • AWS CLI でアクセスする場合 ※ ~/.aws/config に region を指定していない場合は,--region フラグで明示的にリージョン指定する必要がある
    aws s3 --region ap-northeast-1 ls
    aws s3 --region ap-northeast-1 ls s3://test-private-2023-0511
  • curl でアクセスする場合
    $ curl https://[ BUCKET_NAME ].s3.ap-northeast-1.amazonaws.com/test.txt
    $ curl https://test-private-2023-0511.s3.ap-northeast-1.amazonaws.com/test.txt

認証情報の確認

aws configure list

※ プロキシ経由で AWS CLI を使用する場合は,環境変数に no_proxy=169.254.169.254 の設定が必要な場合がある. AWS CLI はインスタンスメタデータを 169.254.169.254 から取得している.169.254.169.254 は EC2 からの直接通信でのみアクセスできる AWS のシステムのアドレスである. OS 側でプロキシを設定している場合,AWS CLI は EC2 に割り当てた IAM role の認証情報を,プロキシ経由で取得しようとするため,システムから読み込めず,None と表示される.

goofys のインストール

順番にインストールして設定する

fuse

fuse は goofys が使用するパッケージ

sudo yum install -y fuse

goofys

※先にfuseをインストールしておく

sudo curl -L https://github.com/kahing/goofys/releases/latest/download/goofys -o /usr/local/bin/goofys
sudo chmod a+x /usr/local/bin/goofys
goofys --version

goofys で S3 のマウント

test-2023-0511 バケットを S3 に作成しておく.

mkdir test-2023-0511-bucket
goofys --region=ap-northeast-1 test-2023-0511 $HOME/test-2023-0511-bucket

--region=ap-northeast-1 を付けないと,Gateway 型の S3 Endpoint 経由でなく,NAT Instance 経由で通信するため,NAT Instance を停止させた場合にマウントできない.

マウント状況の確認

$ df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
devtmpfs         215M   68K  215M    1% /dev
tmpfs            226M     0  226M    0% /dev/shm
/dev/nvme0n1p1   7.8G  1.2G  6.6G   16% /
test-2023-0511   1.0P     0  1.0P    0% /home/admis/test-2023-0511-bucket

失敗した場合は,エラーログを見る

sudo cat /var/log/messages

なお,永続化する場合は,下記の要領らしい.(未検証)

$ cat /etc/fstab
/root/go/bin/goofys#testbucket /mnt/testbucket fuse _netdev,allow_other,--dir-mode=0775,--file-mode=0666,--uid=1000,--gid=1000,--region=ap-northeast-1 0 0

> Ref: [VPCエンドポイントでS3にアクセスする場合のgoofys利用で気を付けておきたいこと](https://dev.classmethod.jp/articles/20190208-goofys/)

参考資料

cloud-init のログを確認する

sudo cat /var/log/cloud-init-output.log

参考情報

CDK で EC2 に IAM Role を割り当てる

  1. CfnInstance で EC2 を作成する場合 EC2InstanceRole.create() で role を作成して,CfnInstanceProfile を通した後,CfnInstance に渡す(らしい).
  1. ec2.Instance で EC2 を作成する場合 ソースコード参照

S3 Bucket Name のパラメータストアを通した連携

  • パラメータの設定例
    const s3_bucket = new cdk.aws_s3.Bucket(this, 'test-bucket-to-mount-from-ec2', {
      bucketName: 'test-bucket-to-mount-from-ec2',
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      autoDeleteObjects: true,
    });
    const ssmParameter = new cdk.aws_ssm.StringParameter(this, 'aws_s3_bucket_name', {
      parameterName: '/s3_bucket_name_to_mount_on_ec2/001',
      stringValue: s3_bucket.bucketName,
    });
  • AWS CLI からのパラメータ取得 全てのパラメータの表示
    aws ssm get-parameters-by-path --path "/" --recursive --region ap-northeast-1 --region ap-northeast-1
    指定したパラメータの取得
    aws ssm get-parameter --name /s3_bucket_name_to_mount_on_ec2/001 --query "Parameter.Value" --region ap-northeast-1 | tr -d '"'

※ EC2 からアクセスする場合は AmazonSSMReadOnlyAccess 権限を付与しておく.


付録

よく使うコマンド

プロジェクトの初期化

$ mkdir cdk-workshop && cd cdk-workshop
$ cdk init sample-app --language typescript

テスト

npx npm run test
npx npm run test -- -u

デプロイ

npx cdk synth
npx cdk deploy --all --require-approval never
npx cdk destroy --all --force

aws-cdk-private-subnet-ec2-and-s3-goofys's People

Contributors

admiswalker avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.