{"id":23270421,"url":"https://github.com/umanari145/iac","last_synced_at":"2026-05-05T12:31:57.576Z","repository":{"id":148908024,"uuid":"269963435","full_name":"umanari145/IaC","owner":"umanari145","description":"Infrastructure as Code(terraform)","archived":false,"fork":false,"pushed_at":"2024-12-28T06:46:40.000Z","size":151,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T09:46:07.385Z","etag":null,"topics":["aws","ci-cd","codedeploy","ec2","ecs","terraform","vpc"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/umanari145.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-06T11:42:39.000Z","updated_at":"2024-12-28T06:46:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"c2bb52e3-b7cd-44ca-a91d-07613241e912","html_url":"https://github.com/umanari145/IaC","commit_stats":{"total_commits":83,"total_committers":1,"mean_commits":83.0,"dds":0.0,"last_synced_commit":"22edc110225892cb283167220de6eae1795586b8"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/umanari145/IaC","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umanari145%2FIaC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umanari145%2FIaC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umanari145%2FIaC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umanari145%2FIaC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/umanari145","download_url":"https://codeload.github.com/umanari145/IaC/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umanari145%2FIaC/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32649532,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-05T11:29:49.557Z","status":"ssl_error","status_checked_at":"2026-05-05T11:29:48.587Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","ci-cd","codedeploy","ec2","ecs","terraform","vpc"],"created_at":"2024-12-19T18:51:33.709Z","updated_at":"2026-05-05T12:31:57.543Z","avatar_url":"https://github.com/umanari145.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Infrastructure as Code\n\n## terraform\n\n参考リンク\u003cbr\u003e\nhttps://qiita.com/bunty/items/5ceed66d334db0ff99e8\u003cbr\u003e\nhttps://blog.dcs.co.jp/aws/20210401-terraformaws.html\n\n### ファイル構成\n\n.teffaorm.lock.hcl・・キャッシュファイル\u003cbr\u003e\n.terraform.tfstate・・現状の状況を記載(composer.lock に近い)\n\n### インストール\n\nterraform 本体のインストール\n\n```\nbrew install terraform\nterraform --version\n#Terraform v0.12.26でOK\n```\n\ntfenv(terraform のバージョン切り替え)\n\n```\n#個別に元々インストールしていた場合は以下のコマンドでアンインストール\nbrew unlink terraform\n\nbrew install tfenv\ntfenv --version\n# tfenv 2.2.2\n\n\n#想定のバージョンをインストール\ntfenv list-remote\n# バージョンがずらずらと・・\n#その中で選んでインストール\ntfenv install 1.0.0\n# インストールずみのバージョンを表記\ntfenv list\n#  1.0.0\n# No default set. Set with 'tfenv use \u003cversion\u003e' //まだ設定されていない\n# 実際に仕様\ntfenv use 1.0.0\n\n# 使用後確認するとシンボリックリンクを貼る\ntfenv list\n* 1.0.0 (set by /usr/local/Cellar/tfenv/2.2.2/version)\n```\n\n### atom プラグイン\n\n- language-terraform:シンタックスハイライト(色つけ)\n- linter-terraform-syntax:構文チェック\n- terraform-fmt:整形ツール\n\n### vscode\n\n- Draw.io Integration: draw をエディタ上に表現\n\n下記のコマンドは全て terraform ディレクトリにおりて実行する\n\n### ファイル構成(各プロバイダーごと)\n\n共通系\n\n- chart 構成を drawio で保存\n- .terraform.lock.hcl ライブラリの状態保存\n- terraform.tfstate.\\* 状態のファイル(AWS の状態がここに保存される)\n- provider.tf aws アカウントの情報\n- variables.tf(変数の格納)\n\n- APIgateway\n\n  - APIgateway のサンプル\n    - \\*\\*\\_rest_api API 本体\n    - \\*\\*\\_resource\n    - https://dev.classmethod.jp/articles/terraform-deployment-rest-api-gateway\n    - https://colabmix.co.jp/tech-blog/terraform-api-gateway/\n\n- aws_ec2\n\n  - vpc ネットワーク関連(ゲートウェイ、ルートテーブル、サブネットマスク、セキュリティグループなど)\n  - ec2 ec2-Instance コマンドについて\n  - keygen 鍵の生成(秘密鍵と公開鍵キーペアの作成)\n  - elb ロードバランサーの設定\n\n- aws_ecs\n  - ecr ecr の作成\n    - ecr.tf リポジトリ\n    - app nginx の Docker\n    - nginx web の Docker\n    - src 実際にデプロイされるファイル\n    - aws_ecr_lifecycle_policy ライフサイクルの json\n    - ecr_push.sh ECR のプッシュのシェル\n    - provider.tf (../provider.tf のシンボリックリンク)\n  - ecs ecs の作成\n    - ecs.tf クラスター、サービス、タスクの登録\n    - iam.tf ecs に必要な IAM\n    - vpc.tf ネットワーク関連\n    - lb アプリケーションロードバランサー\n\nhttps://hi1280.hatenablog.com/entry/2023/04/07/200303\n\nhttps://qiita.com/Shoma0210/items/b998a260c5d18839fb7a\n\n- aws_cicd (ECR構築は別)\n\n  - main.tf 実行ファイル\n  - variables.tf プロジェクトのプレフィックスなど\n\n```\n#ecr構築\ncd aws_ecs/ecr\nterraform apply\n# ローカルコンテナ作成\ndocker compose build --no-cache\n# awsの変数の読み込み後\nbash ecr_push.sh\n# vpc〜ecs構築\ncd aws_cicd\nterraform apply -var project_pre=\"*******\" -var-file terraform.vfvars -lock=false\n```\n\n- azure(やや頓挫気味)\n\n  - main.tf (リソースグループ、Vnet、サブネット、パブリック IP、アプリケーションゲートウェイまで。ただし http_listener に関しては何度も作り直しが発行されてしまう。)\n  - acr_aks.tf(別サービスが自動的に作られうまく紐づかない・・・のと失敗したのに aks が作例されてしまっている)\n\nazure に関しては provider でアカウント情報をコントロールするのではなく、az login 後おこなう(以下のようなページがでます)\n\n```\n[\n  {\n    \"cloudName\": \"AzureCloud\",\n    \"managedByTenants\": [],\n    \"name\": \"Azure サブスクリプション 1\",\n    \"state\": \"Enabled\",\n    \"user\": {\n    }\n  },\n  {\n\n  }\n]\n```\n\n- ディレクトリ自体がプロジェクトのようなもの\n  aws 用に./aws を作った場合にはここで terraform (init|plan|apply|show)コマンドをとる\n\n- cloudformation\n  - main.yaml gw-rm-psn\\*1 (gateway-routetable-PublicSubnet1)\n  - inc_parivate_subnet.yaml(gatewa-routetab-le-PublicSubnet1-PrivateSubnet2 PrivateSubnet3 3 は孤立状態)\n\n#### 1 terraform init\n\nディレクトリごとに行うため git に近い\n\nec2.tf ファイルと variables.tf を作成し、一番最初に打つコマンド(何もないと動かない)\u003cbr\u003e\naws のアカウント情報などの初期化をしている(composer install などに近いイメージ)\u003cbr\u003e\n下記のようなメッセージが出れば OK\u003cbr\u003e\nまた.terraform にライブラリがインストールされる\n\n```\n\nInitializing the backend...\n\nInitializing provider plugins...\n\nThe following providers do not have any version constraints in configuration,\nso the latest version was installed.\n\n・・・・・・・・・・・・・・・・・・・・・・・\n\ncommands will detect it and remind you to do so if necessary.\n```\n\nvariable.tf などが同階層にあると下記のようなエラーがでる\u003cbr\u003e\naws アカウントの重複\n\n```\nError: Duplicate provider configuration\n```\n\n#### 2 terraform plan\n\nオプション `-var-file ***.tfvars`で変数を取り込むことができる\n\n構文チェックと新規に追加された要素の確認(確認事項)\n\n```\nprovider.aws.region\n  The region where AWS operations will take place. Examples\n  are us-east-1, us-west-2, etc.\n\n  Enter a value: us-east-1\n\nRefreshing Terraform state in-memory prior to plan...\nThe refreshed state will be used to calculate this plan, but will not be\npersisted to local or remote state storage.\n\n\n------------------------------------------------------------------------\n\nAn execution plan has been generated and is shown below.\nResource actions are indicated with the following symbols:\n  + create\n\nTerraform will perform the following actions:\n\n  # aws_instance.web1 will be created\n  + resource \"aws_instance\" \"web1\" {\n・・・・・・・・\nPlan: 1 to add, 0 to change, 0 to destroy.\n\n------------------------------------------------------------------------\n\nNote: You didn't specify an \"-out\" parameter to save this plan, so Terraform\ncan't guarantee that exactly these actions will be performed if\n\"terraform apply\" is subsequently run.\n\n```\n\nもしすでに適用されているとすると以下のようなメッセージが出る(基本的には terraform.tfstate と実機の差分を見ている)\n\n```\nNo changes. Your infrastructure matches the configuration.\n\nTerraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.\n\n```\n\nちなみにエラーの場合は以下のようなメッセージが出る\n\n```\n╷\n│ Error: Reference to undeclared resource\n│\n│   on vpc.tf line 28, in resource \"aws_route_table\" \"sample_rtb\":\n│   28:     gateway_id = aws_internet_gateway.sample_igw.id\n│\n│ A managed resource \"aws_internet_gateway\" \"sample_igw\" has not been declared in the root module.\n```\n\n#### 3 (sudo) terraform apply\n\nplan 同様オプション `-var-file ***.tfvars`で変数を取り込むことができる\n\n権限変更などで sudo が必要なケースがある\n\n実際の実行。\nAWS の管理画面を見るとインスタンスが実際に立ち上がっている\n\n```\nprovider.aws.region\n  The region where AWS operations will take place. Examples\n  are us-east-1, us-west-2, etc.\n\n  Enter a value: us-east-1\n\n\nAn execution plan has been generated and is shown below.\nResource actions are indicated with the following symbols:\n  + create\n\nTerraform will perform the following actions:\n\n  # aws_instance.web1 will be created\n  + resource \"aws_instance\" \"web1\" {\n      + ami                          = \"ami-0323c3dd2da7fb37d\"\n    }\n\nPlan: 1 to add, 0 to change, 0 to destroy.\n\nDo you want to perform these actions?\n  Terraform will perform the actions described above.\n  Only 'yes' will be accepted to approve.\n\n  Enter a value: yes\n\naws_instance.web1: Creating...\naws_instance.web1: Still creating... [10s elapsed]\naws_instance.web1: Still creating... [20s elapsed]\naws_instance.web1: Still creating... [30s elapsed]\naws_instance.web1: Creation complete after 31s [id=i-0a2edbaf3f18d8eb0]\n\nApply complete! Resources: 1 added, 0 changed, 0 destroyed.\n```\n\nこの時点で terraform.tfstate が作られる(状態を管理?)\n\nファイルを更新して、apply をすると差分ができるので実行される\n\n#### 4 terraform show\n\n実際のリソースの状態を出力する(terraform.tfstate の中身をわかりやすく出力している)\u003cbr\u003e\n以下のようにリソースを指定することができる\n\n```\nterraform show aws_ecr_repository.ecr-nginx:\n```\n\n```\n# aws_instance.web1:\nresource \"aws_instance\" \"web1\" {\n    実際のインスタンスの実データ\n```\n\n#### 5 terraform destory\n\n既存のコードの消去\u003cbr\u003e\n建てたものも完全に消去される\n\n```\nprovider.aws.region\n  The region where AWS operations will take place. Examples\n  are us-east-1, us-west-2, etc.\n\n  Enter a value: us-east-1\n\naws_instance.web1: Refreshing state... [id=i-0a2edbaf3f18d8eb0]\n\nApply complete! Resources: 0 added, 0 changed, 0 destroyed.\nmatsumotogaseinoMacBook-ea:terraform matsumoto$ terraform destroy\nprovider.aws.region\n  The region where AWS operations will take place. Examples\n  are us-east-1, us-west-2, etc.\n\n  Enter a value: us-east-1\n\naws_instance.web1: Refreshing state... [id=i-0a2edbaf3f18d8eb0]\n\nAn execution plan has been generated and is shown below.\nResource actions are indicated with the following symbols:\n  - destroy\n\nTerraform will perform the following actions:\n\n  # aws_instance.web1 will be destroyed\n  - resource \"aws_instance\" \"web1\" {\n        実際のインスタンスの実データ\n    }\n\nPlan: 0 to add, 0 to change, 1 to destroy.\n\nDo you really want to destroy all resources?\n  Terraform will destroy all your managed infrastructure, as shown above.\n  There is no undo. Only 'yes' will be accepted to confirm.\n\n  Enter a value: yes\n\naws_instance.web1: Destroying... [id=i-0a2edbaf3f18d8eb0]\naws_instance.web1: Still destroying... [id=i-0a2edbaf3f18d8eb0, 10s elapsed]\naws_instance.web1: Still destroying... [id=i-0a2edbaf3f18d8eb0, 20s elapsed]\naws_instance.web1: Still destroying... [id=i-0a2edbaf3f18d8eb0, 30s elapsed]\naws_instance.web1: Destruction complete after 34s\n\nDestroy complete! Resources: 1 destroyed.\n```\n\n### tips\n\n特定の resource を指定したいとき\n\n```\nterraform (plan or apply or delete) --target={リソース名}.{リソースにつけた独自の名前}\n# 例　terraform apply --target=aws_vpc.main\n```\n\n複数の profile を使い分けたいとき(下記をセットしておく)\n\n```\nexport AWS_PROFILE=\u003cprofile名\u003e\n```\n\nLocal Values とはモジュール内に閉じて使える変数。モジュール内でのローカル変数のようなもの。\n\n```\n# 許可IPなど(一般的には自分のIP)\nlocals {\n  admit_ip  = \"113.149.17.185\"\n  azs = [\"us-east-1a\",\"us-east-1b\"]\n  cidr_block = [\n    \"10.0.1.0/24\", \"10.0.2.0/24\"\n  ]\n}\n\nresource \"aws_security_group\" \"elb-sg\" {\n  ・・・・・\n  ingress {\n 　　・・・・\n    cidr_blocks = [\"${local.admit_ip}/32\"]\n  }\n```\n\n#### terraform state list リソース一覧\n\n```\nterraform state list\n# tfstateで記載されている一覧を表示　以下例\n# aws_ecr_repository.ecr-nginx\n```\n\n### 一般的なインフラネタ\n\nロードバランサーについて\u003cbr\u003e\nhttps://www.isoroot.jp/blog/4584/\u003cbr\u003e\nhttps://github.com/knakayama/tf-alb-demo\u003cbr\u003e\n\n### chart\n\nインフラ関連の図の資料\nhttps://www.draw.io/\n\nVPC_diagram\n\n- VPC\n- Internet Gateway\n- Route table\n- EC2\n\nEC2_DB_diagram\n\n- VPC\n- Internet Gateway\n- Route table\n- EC2\n- RDS(MySQL)\n\nEC2_sample\n\n- VPC\n- Internet Gateway\n- Route table\n- ALB\n- EC2 × 2\n\nECS\n\n- VPC\n- Internet Gateway\n- Route table\n- ALB\n- ECS\n- Cloud Watch\n\nVPC_NAT\n\n- VPC\n- Internet Gateway\n- Route table\n- NAT\n- Subnet\n\n## terraform validate\n\n設定情報がおかしくないかの確認\n\n```\n╷\n│ Warning: Argument is deprecated\n│\n│ with aws_ecs_cluster.sample_ecs_cluster,\n│ on ecs.tf line 7, in resource \"aws_ecs_cluster\" \"sample_ecs_cluster\":\n│ 7: capacity_providers = [\n│ 8: \"FARGATE\",\n│ 9: \"FARGATE_SPOT\"\n│ 10: ]\n│\n│ Use the aws_ecs_cluster_capacity_providers resource instead\n╵\n╷\n│ Error: Reference to undeclared resource\n│\n│ on iam.tf line 38, in resource \"aws_iam_role_policy_attachment\" \"ecs_task_execution\":\n│ 38: role = aws_iam_role.ecs_task_execution_sample.name\n│\n│ A managed resource \"aws_iam_role\" \"ecs_task_execution_sample\" has not been declared in the root module.\n```\n\n## 変数\n\n### locals の定義と variable\n\n- locals・・ローカル変数で外部から変更負荷\n- variables・・外部から変更可能(コマンドライン実行時やファイル指定)\n\nlocal\n\n```\nlocals {\n  project = \"hogehoge\"\n}\n\nresource \u003cRESOURCE_TYPE\u003e \u003cRESOURCE_NAME\u003e {\n  tags = \"${local.project}-vpc\"\n}\n```\n\nvariable\n\n```\nvariable \"project\" {\n  type = string\n  default = \"hogehoge\"\n}\n\nresource \u003cRESOURCE_TYPE\u003e \u003cRESOURCE_NAME\u003e {\n  tags = \"${var.project}-vpc\"\n}\n```\n\n変数の方としてはプリミティブ以外にオブジェクト型なども\n\n```\nvariable \"message\" {\n  type = string\n  default = \"test\"\n}\n\n# オブジェクト\nvariable \"obj_sample\" {\n  type = object({\n    name = string\n    age = number\n  })\n  default = {\n    name = \"tanaka\"\n    age = 28\n  }\n}\n\n参照時は\nvar.obj_sample.name\n\n#リスト\nvariable \"list_sample\" {\n  type = list(string)\n  default = [\"tanaka\", \"sato]\n}\n\nvar.list_sample[0]\n\n#map\nvariable \"map_sample\" {\n  type = map(string)\n  default = {\n    \"High\" = \"x2.large\"\n    \"Low\" = \"x2.nano\"\n  }\n}\nvar.map_sample.High\n```\n\n### 変数の上書き\n\nvariables で定義してることを前提に・・・\n\n環境変数(TF*VAR*が接頭語)\n\n```\nexport TF_VAR_message\n```\n\n変数ファイル\n\n\\*\\*\\*.vfvars\n\n```\nmessage = \"sample message\"\n\n```\n\n-var での上書き\n\n```\nterraform apply - var message=\"sample message\"\n```\n\n変数の優先度はコマンド引数\u003e変数ファイル\u003e環境変数\n\n### data\n\nすでに定義されている値を定数的に定義する\n\n```\ndata\"aws_iam_user\"\"my_user\"{\n  user_name=\"my_user_name\"\n}\n```\n\n## ECS に関して\n\n- オートスケーリングの設定の必要あり\n- スケーリングポリシー\n\n### 負荷テスト\n\napache ベンチや JMeter など動的に負荷をかけられるサービスを使う\n\n総合試験でのテスト内容など(\nhttps://skill-up-engineering.com/2022/01/29/%e7%b7%8f%e5%90%88%e8%a9%a6%e9%a8%93%e3%81%a7%e3%81%ae%e3%83%86%e3%82%b9%e3%83%88%e5%86%85%e5%ae%b9%e3%81%aa%e3%81%a9/)\n\n## drawIO おすすめプラグイン\n\ndrawIO Integration\n\n## awspec\n\nインフラのテストツール\n\u003cbr\u003e2024/07 現在の ruby bundler の version\n\n```\nroot@7d4fac1b71bf:/home/app# ruby -v\nruby 3.3.3 (2024-06-12 revision f1c7b6f435) [x86_64-linux]\nroot@7d4fac1b71bf:/home/app# bundler -v\nBundler version 2.5.11\n```\n\n## cloudformation\n\ncli コマンド\u003cbr\u003e\nhttps://dev.classmethod.jp/articles/read-aws-cli-cfn-options/\n\nスタック・・単一のリソースではなく、リソースをグループ化したもの\n\n確認\n\n```\naws cloudformation validate-template \\\n --template-body file://main.yaml\n\n# レスポンス\n{\n    \"Parameters\": []\n}\n```\n\n作成\n\n```\naws cloudformation create-stack \\\n  --template-body file://main.yaml \\\n  --stack-name mynetwork\n# レスポンス\n{\n    \"StackId\": \"arn:aws:cloudformation:us-west-1:xxxxx:stack/mypvc/zzzzzzzzz\"\n}\n```\n\n変更時は一気に更新するよりは変更セットを作ってみた方が差分が確認しやすい。\n\n変更セットを作成\n\n```\naws cloudformation create-change-set \\\n --stack-name  mynetwork3 \\\n --template-body file://vpc.yaml  \\\n --change-set-name addSubnet\n\n# レスポンス\n{\n    \"Id\": \"arn:aws:cloudformation:us-west-1:xxxx:changeSet/addSubnet/yyyy\",\n    \"StackId\": \"arn:aws:cloudformation:us-west-1:xxxx:stack/mynetwork3/yyyy\"\n}\n```\n\n変更セットの確認(差分が確認できる)\n\n```\naws cloudformation describe-change-set \\\n  --change-set-name addSubnet \\\n  --stack-name mynetwork3\n\n# レスポンス\n{\n    \"Changes\": [\n        {\n            \"Type\": \"Resource\",\n            \"ResourceChange\": {\n                \"Action\": \"Add\",\n                \"LogicalResourceId\": \"RouteTable02a\",\n                \"ResourceType\": \"AWS::EC2::RouteTable\",\n                \"Scope\": [],\n                \"Details\": []\n            }\n        },\n        {\n・・・・・・・・\n\n```\n\n変更セットの確定\n\n```\naws cloudformation execute-change-set \\\n --change-set-name addSubnet \\\n --stack-name mynetwork3\n\n# レスポンスは特にない\n```\n\n一気に更新\n\n```\naws cloudformation update-stack \\\n  --template-body file://main.yaml \\\n  --stack-name mynetwork\n```\n\nデプロイ\n\n```\naws cloudformation deploy \\\n  --template-file main.yaml \\\n  --stack-name mynetwork\n\nWaiting for changeset to be created..\nWaiting for stack create/update to complete\nSuccessfully created/updated stack - mypvc\n```\n\nスタックの削除\n\n```\naws cloudformation delete-stack --stack-name mynetwork\n```\n\nスタックの一覧\n\n```\naws cloudformation describe-stacks\n```\n\n### modules\n\nパーツ化して、別の箇所から呼び出す\n\nモジュール単位での指摘\nパーツをこの部分で実装\n\n- modules\n  - CICD\n    - build\n      - main.tf\n      - variables.tf\n    - deploy\n      - main.tf\n      - variables.tf\n  - lambda\n    - main.tf\n    - variables.tf\n  - sns_topic\n    - main.tf\n    - variables.tf\n- sns_topic\n  - main.tf\n  - variables.tf\n  - terraform.tfvars\n\nモジュール指定での実行\n\n```\nterraform plan -target=module.lambda\n\n```\n\n### テンプレート内部の変数について\n\nRef 組み込み関数 Ref は、指定したパラメータまたはリソースの値\n\n例)\n!Ref 論理名\nパラメータの論理名を指定すると、それはパラメータの値\u003cbr\u003e\nリソースの論理名を指定すると、それはそのリソースを参照するために通常使用できる値\n\n### ネットワークについて\n\nプレイベート IP\u003cbr\u003e\n10.0.0.0 - 10.255.255.255 (10/8 プレフィックス)\u003cbr\u003e\n172.16.0.0 - 172.31.255.255 (172.16/12 プレフィックス)\u003cbr\u003e\n192.168.0.0 - 192.168.255.255 (192.168/16 プレフィックス)\n\nCIDR ブロックに関して\u003cbr\u003e\nなるべく大きくとった方が多くの AWS リソースを格納できる\n\nインターネットゲートウェイと NAT ゲートウェイ\u003cbr\u003e\nインターネットゲートウェイ・・VPC が外部と接続するために必要なコンポーネント\u003cbr\u003e\nNAT ゲートウェイ・・プライベートサブネットから VPC 外に接続するために使用されるコンポーネント。\u003cbr\u003e\nhttps://qiita.com/KWS_0901/items/8f818caaa4e989a185c5\u003cbr\u003e\nhttps://milestone-of-se.nesuke.com/sv-advanced/aws/internet-nat-gateway/\n\nサブネットに関して\nhttps://www.stylez.co.jp/aws_columns/understand_the_basics_of_aws_networking/understanding_aws_public_subnets_and_private_subnets/\n\n### ポリシーについて\n\n基本的には付与したい複数の許可(ポリシー)をロールに集め、それをリソース(例 Stepfunction)に付与する感じ。\u003cbr\u003e\n\n- AWS 管理ポリシー(ManagedPolicy)　・・AWS に元々付与されているポリシー(AmazonEC2FullAccess 等。)\n- カスタマー管理ポリシー・・ ユーザーが独自に作成するポシリーのこと\n- インラインポリシー・・ユーザー、グループ、ロールに埋め込まれたポリシー(他の IAM に付与できず特定の IAM 専属という感じ。インライン CSS とイメージが近いかも)\n\nポシリー\n\n```\n  MyCustomPolicy:\n    Type: 'AWS::IAM::Policy'\n    Properties:\n      PolicyName: MyCustomPolicy\n      PolicyDocument:\n        Version: '2012-10-17'\n        Statement:\n          - Effect: Allow (Allow or Deny)\n            Action:\n              - 's3:ListBucket' (AWSサービス名:操作)\n              - 's3:GetObject'\n            Resource:\n              - 'arn:aws:s3:::example-bucket'　(適用される範囲)\n              - 'arn:aws:s3:::example-bucket/*'\n      Roles:\n        - Ref: MyExampleRole\n```\n\n### バケットポリシー\n\nS3 のバケットに関するルールを制御する。\u003cbr\u003e\nもしロールにバケットに関するポリシーがかかれており、付与されていればいらない。\n(どちらか片方で良い。)\u003cbr\u003e\nただしクロスアカウント(異なるアカウント間でのバケットのやりとり)の場合、ロールとポリシー両方に権限の記載がなければいけない\u003cbr\u003e\nhttps://www.bioerrorlog.work/entry/s3-bucket-policy-vs-iam-policy\n\n### ロールについて\n\nロール\n\n```\n  CodeDeployServiceRole:\n    Type: \"AWS::IAM::Role\"\n    Properties:\n      RoleName: \"CodeDeployServiceRole\"\n      # CodeDeployが他のサービスにアクセスする\n      Description: \"Allow CodeDeploy to call AWS all service\"\n      AssumeRolePolicyDocument:\n        Version: \"2012-10-17\"\n        Statement:\n          - Effect: \"Allow\"\n            Principal:\n              Service:\n                - \"codedeploy.amazonaws.com\" (CodeDeployサービスがこのロールを引き受けることを許可します。)\n            Action:\n              - \"sts:AssumeRole\" (このロールを引き受けるアクションを許可します。)\n      ManagedPolicyArns:\n        - \"arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole\" (このロールにアタッチされる管理ポリシーのARN)\n```\n\n### サービスロール\n\nユーザーに無制限に操作権限を与えるとまずいので、AWS のあるサービスに対して、別のサービスへの操作権限を許可\nhttps://qiita.com/sakuraya/items/920b9d0b549c8c412416\u003cbr\u003e\n\n### CodeDeploy\n\n- cloudformation\n  - coude_deploy(EC 作成時には main.yaml で VPC をつくる)\n    - appspec.yaml\n    - CodeDeploy.yaml\n    - EC2\n    - IAM.yaml\n    - S3.yaml\n\nCodeDeploy\u003cbr\u003e\nhttps://qiita.com/tsukapah/items/598ef327ccc51b4955b6\u003cbr\u003e\nhttps://qiita.com/terukizm/items/e2c1400d129042868731\u003cbr\u003e\nhttps://jitera.com/ja/insights/9418\n\u003cbr\u003e\nEC2\u003cbr\u003e\nhttps://qiita.com/tyoshitake/items/c5176c0ef4de8d7cf5d8\u003cbr\u003e\n\n```\naws cloudformation create-stack \\\n  --template-body file://IAM.yaml \\\n  --stack-name code_deploy_IAM \\\n  --capabilities CAPABILITY_NAMED_IAM\n\n aws cloudformation create-stack \\\n  --template-body file://S3.yaml \\\n  --stack-name codeDeployS3\n```\n\nappspec とは・・・デプロイプロセスを定義するための設定ファイル\u003cbr\u003e\n\nBeforeInstall: アプリケーションの新しいバージョンをインスタンスにコピーする前に実行されるスクリプト。\u003cbr\u003e\nAfterInstall: アプリケーションの新しいバージョンがインスタンスにコピーされた後に実行されるスクリプト。\u003cbr\u003e\nApplicationStart: アプリケーションが開始された後に実行されるスクリプト。\u003cbr\u003e\nValidateService: デプロイが正常に完了したことを検証するために実行されるスクリプト。\n\n## CICD がらみ\n\nhttps://qiita.com/suiwave/items/e37338031c07e4dee52a#tf%E3%82%B3%E3%83%BC%E3%83%89-1\nhttps://qiita.com/okubot55/items/3cbeb4acb0ffa9ddd961\nhttps://github.com/okubo-t/aws-tf-fargate-blue-green-deploy\nhttps://qiita.com/suzuki0430/items/8b6e99669a8d4242a5ce\nhttps://qiita.com/tarian/items/75064f7c33bebcd5ace4\nhttps://dev.classmethod.jp/articles/ecs-deploy-with-codepipeline/\nhttps://qiita.com/suzuki0430/items/06755312a7757b940e69\n\n## 参考教材\n\n「AWS CloudFormation を使って VPC 環境を構築してみよう！」(https://www.techpit.jp/courses/77)\u003cbr\u003e\n「AWS と Terraform で実現する Infrastructure as Code」(https://www.udemy.com/course/iac-with-terraform/)\u003cbr\u003e\n「AWS で Docker を本番運用！AmazonECS を使って低コストでコンテナを運用する実践コース」(https://www.udemy.com/course/ecsfargate/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fumanari145%2Fiac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fumanari145%2Fiac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fumanari145%2Fiac/lists"}