質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Terraform

Terraformは、インフラ構築のためのツールです。AWS/DigitalOcean/GoogleCloudといった様々なインフラに対応。インフラ構成のコード管理や変更の作業などの手間を自動化し、インフラ構築の効率化を図ることができます。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

2098閲覧

アクセスキーとシークレットキーをmain.tfに直接記述しない方法をしりたい。

pay_561

総合スコア26

Terraform

Terraformは、インフラ構築のためのツールです。AWS/DigitalOcean/GoogleCloudといった様々なインフラに対応。インフラ構成のコード管理や変更の作業などの手間を自動化し、インフラ構築の効率化を図ることができます。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2021/03/04 02:51

編集2021/03/04 06:35

##前提・困っていること
Terraformでインフラを構築しGitHubに公開したいのですが、このままだとaccess_keyとsecret_keyが
全世界に公開することになってしまいます。

ローカルでクレデンシャル情報を登録したのですが、terraform planするとエラーになってしまいます。
main.tfに直接記述することでterraform planで確認できました。

$ export AWS_ACCESS_KEY_ID='xxxxxxxxxxxxx' $ export AWS_SECRET_ACCESS_KEY='xxxxxxxxxxxxxxxxxx'

##エラー

Error: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid. status code: 403, request id: ☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓ on main.tf line 2, in provider "aws": 2: provider "aws" {

##main.tf

# AWS設定 provider "aws" { region = var.aws["region"] profile = var.aws["profile"] # access_key = var.aws["access_key"] # secret_key = var.aws["secret_key"] }

##variable.tf

variable "aws" { default = { profile = "default" region = "ap-northeast-1" # access_key = "☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓" # secret_key = "☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓" } }

##追記
~/.aws/config ~/.aws/credentialsに正しく設定されていないのが原因でした。
ですが、terraform planを実行すると下記のエラーが発生しました。

provider.aws.region The region where AWS operations will take place. Examples are us-east-1, us-west-2, etc. Enter a value: ap-northeast-1 Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ------------------------------------------------------------------------ Error: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid. status code: 403, request id: 4e474dc1-bf31-4383-93ea-4c26c368c092 on <input-prompt> line 1: (source code not available)

~/.aws/credentials

[default] aws_access_key_id = ☓☓☓☓☓☓☓☓☓☓☓ aws_secret_access_key = ☓☓☓☓☓☓☓☓☓☓☓

~/.aws/config

[default] region = ap-northeast-1 output = json

##main.tf

provider "aws" { profile = var.aws["profile"] }

##variable.tf

variable "aws" { default = { profile = "default" } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ドキュメントをまず読んでみましょう

AWS Provider

The AWS provider offers a flexible means of providing credentials for authentication. The following methods are supported, in this order, and explained below:

Static credentials

Environment variables
Shared credentials/configuration file
CodeBuild, ECS, and EKS Roles
EC2 Instance Metadata Service (IMDS and IMDSv2)

Terraformに限らず、他のAWSを利用するツールも同様です。これはSDKの仕様なので。

方法としては

  • ローカルユーザのcredential情報(~/.aws/config~/.aws/credentials)をあらかじめ設定しておき、Terraformにはconfigで設定したprofile名のみ記載する
  • 環境変数を使用してキー、またはプロファイルを設定する

もし仮にAWS上で実行するなら

  • IAMロールの権限を継承して使用する

もできます。(AWS上で動かす場合は基本的にこれが望ましい)

エラーが出てる原因はそこだけだとまだわかりませんが…そもそもちゃんとcredentialsを設定していますか?

投稿2021/03/04 03:00

yu_1985

総合スコア7440

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pay_561

2021/03/04 04:05

ご回答ありがとうございます。 yu_1985さんがおっっしゃった通りcredentials設定が正しく設定されていませんでした。 ですがまた違うエラーが発生してしまいました。 エラー箇所を追記しましたのでご教授いただけたら幸いです。 よろしくお願いします。
yu_1985

2021/03/04 05:23

使っているキーのユーザに十分な権限はありますか? また、通常はリージョンをprofileのほうで指定しているのでprofileを指定しているならterraformのawsプロバイダでの設定は不要です。
yu_1985

2021/03/04 05:44

また、credentialsが正しく設定されているかが不明です。 キーの情報が入るのでそのままは載せられないでしょうけど、できればキーの情報をマスクした状態でどう設定しているか載せてください。
pay_561

2021/03/04 06:37

ユーザの権限はAdministratorAccess一つだけ権限を与えています。 credentialsの情報を追記しましたのでよろしくお願いします。
yu_1985

2021/03/04 08:39

エラーの内容的に権限に問題があるか正しく鍵を指定できていないかいずれかの可能性が高そうですが…。 ちなみに、その状態でaws cliを使うことはできますか? aws configure list を実行するとどうなりますでしょうか。 また、ドキュメントに書いてあるように明示的にshared_credentials_file を指定(相対パスだと怪しいので絶対パス指定で)したらどうなりますか?この場合credentialsの方です。
pay_561

2021/03/04 09:07

度々すみません aws configure listを実行しましたら下記のように表示されました。 Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************BIML shared-credentials-file secret_key ****************8C/Q shared-credentials-file region ap-northeast-1 config-file ~/.aws/config また、明示的にshared_credentials_file を指定したら同じエラー結果が帰ってきました。 credentials [default] aws_access_key_id = ☓☓☓☓☓☓☓☓☓☓☓☓ aws_secret_access_key = ☓☓☓☓☓☓☓☓☓☓☓☓ shared_credentials_file = "/Users/tf_user/.aws/creds" $ terraform plan provider.aws.region The region where AWS operations will take place. Examples are us-east-1, us-west-2, etc. Enter a value: ap-northeast-1 Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ------------------------------------------------------------------------ Error: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid. status code: 403, request id: 4e709d5f-ddd9-4b00-ad15-3b81ee6164e9 on <input-prompt> line 1: (source code not available)
yu_1985

2021/03/04 09:12 編集

> shared_credentials_file = "/Users/tf_user/.aws/creds" これはあなたがcredentialを設定したファイルではないですよね? ~/.aws/credentialsをフルパスで書いてみてください。 また、それはterraformのほうに書くのであってcredentialsの中に書くのではないです。 ドキュメントをご参照ください。
yu_1985

2021/03/04 09:15

もっとも、aws configure listでprofileを指定しないでその結果が帰ってくるのであればshared-credentialsのパスが間違っているわけでもなさそうですね。 terraformのエラーで足りないと言われているのはsts:GetCallerIdentityなので aws sts get-caller-identity で、ちゃんと想定したユーザのキーを使っているかを確認してください。
pay_561

2021/03/04 09:16

すみません解決できました。 yu_1985さんがおっっしゃった通り正しく鍵を指定できていなかったようです。 長い時間ご回答下さりありがとうございました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問