Comments (4)
Failed to query available provider packagesエラーについて
初歩的なミスだけどスタックしたのでメモ
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
provider "aws" {
region = "ap-northeast-1"
}
こんな感じのことを書くが、その際にhashicorp/aws
を引っ張ってくる
しかしリソースにtypoで
resource "awas_instance" "sample" {
ami = "ami-0ba46945fe4414e8a"
instance_type = "t2.small"
}
と書いてしまうとawas
を引っ張ってこようとして存在しないのでエラーになる
つまりTerraformはリソースのprefixをみてproviderを取ってくる
なのでtypoには注意
参照
from self-study.
outputについて
Terraformでは、他ファイルのリソースは同じディレクトリ内でしか参照することができない。
そのため、別ディレクトリにあるリソースを参照するにはoutput
を用いる
①output
でエクスポートする変数を宣言する
# "instance_ip_addr"が名前
output "instance_ip_addr" {
value = aws_instance.server.private_ip #エクスポートするリソース
}
②terraform_remote_state
を使う
data "terraform_remote_state" "vpc" {
backend = "s3"
config = {
region = "ap-northeast-1"
}
}
③エクスポートされたリソースを使う
data.terraform_remote_state.[②で指定した名前].outputs.[①で指定した名前]
で参照することができる
resource "aws_instance" "foo" {
# ...
subnet_id = data.terraform_remote_state.vpc.outputs.instance_ip_addr
}
参照
from self-study.
for_each
について
Terraformでのループ処理。リソースを複数作成するときとかに使う。
for_each
はMeta-Argumentで、moduleやresourceで使用可能。
resource "azurerm_resource_group" "rg" {
for_each = {
a_group = "eastus"
another_group = "westus2"
}
name = each.key
location = each.value
}
for_each
はmap
かset(string)
しか受け取ることができない。list
やtuple
は受け取ることができない。for_each = ["hoge", "fuga"]
みたいな形は無理for_each = toset(["hoge", "fuga"])
ならOK
- 同じresource内で
each.key
やeach.value
として参照できる for_each
はfor
と共に使うことが多い
locals {
images = [
{ name = "foo", image = "alpine" },
{ name = "bar", image = "debian" },
]
}
resource "docker_container" "this" {
for_each = { for i in local.images : i.name => i }
name = each.value.name
image = each.value.image
}
参照
- The for_each Meta-Argument
- Terraformのfor_eachにmapのlistを渡してループしたい
- Terraformでのloop処理の書き方(for, for_each, count)
from self-study.
depends_on
について
terraform apply
するとき、どういう順序でリソースが作成されるかはTerraformが自動的に決めてくれる- しかし稀にちゃんと順序を手動で決める必要がある
depends_on
で指定したリソースが作られたあとにそのリソースが作られる- ALBとECS Serviceを接続するときに順序をちゃんと指定する必要があった(リスナーを先に作る必要があった)
resource "aws_ecs_service" "webapp-service" {
name = "sample-service"
cluster = aws_ecs_cluster.sample-ecs-cluster.id
task_definition = aws_ecs_task_definition.sample-task.arn
desired_count = 1
launch_type = "EC2"
load_balancer {
target_group_arn = aws_lb_target_group.http.arn
container_name = "sample-webapp"
container_port = "3000"
}
depends_on = [
aws_lb_listener.http #リスナーが作られた後にECS Serivceが作られる
]
}
参照
from self-study.
Related Issues (11)
- React + TypeScript開発で調べたことのまとめ HOT 28
- 開発の進め方に関する学習 HOT 3
- JavaScriptに関して調べたことのまとめ HOT 16
- PATHのお話 HOT 2
- いろいろな概念・用語 HOT 4
- Dockerについて HOT 5
- CSSとか、CSS in JSとか HOT 14
- Node.jsについて HOT 3
- AWSとか HOT 3
- GitHub Actionsについて
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from self-study.