使用Terraform脚本旋转带有键的ec2实例时出现错误



我想用一个密钥配置一个ec2实例,并在ec2实例中运行一个脚本。

filenameinstance.tf

resource "aws_key_pair" "mykey" {
key_name = "terraform-nverginia"
public_key = "${file ("${var.PATH_TO_PUBLIC_KEY}")}"
}   
resource "aws_instance" "demo" {
ami = "${lookup (var.AMIS, var.AWS_REGION)}"
instance_type = "t2.micro"
key_name = "${aws_key_pair.mykey.key_name}"
tags = {
Name = "T-instance"
}
provisioner "file" {   // copying file from local to remote server
source = "deployscript.sh"
destination = "/home/ec2-user/deploy.sh"                           //check if both the file names are same or not.
}
provisioner "remote-exec" {                                          // executing script to do some deployment in the server.
inline = [
"chmod +x /home/ec2-user/deploy.sh",
"sudo /home/ec2-user/deploy.sh"
]
}
connection {   
type = "ssh"                                                      // To connect to the instance
user = "${var.INSTANCE_USERNAME}"
host = "122.171.19.4"      // My personal laptop's ip address
private_key = "${file ("${var.PATH_TO_PRIVATE_KEY}")}"
}
} // end of resource aws_instance
//-------------------------------------------------

filename:provider.tf

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.9.0"
}
}
}

filenamevars.tf

variable "AWS_ACCESS_KEY" {}
variable "AWS_SECRET_KEY" {}
variable "AWS_REGION"{
default = "us-east-1"
}
variable "AMIS" {
type = map
default = {
us-east-1 = "ami-0574da719dca65348"
us-east-2 = "ami-0a606d8395a538502"
}
}
variable "PATH_TO_PRIVATE_KEY" {
default = "terraform-nverginia"
}
variable "PATH_TO_PUBLIC_KEY"{
default = "mykey.pub"
}
variable "INSTANCE_USERNAME"{
default = "ec2-user"
}
filename = terraform.tfvars
AWS_ACCESS_KEY = "<Access key>"
AWS_SECRET_KEY = "<Secret key>"

错误:

PS D:\Rajiv\DevOps-Practice\Terraform\demo-2> terraform plan
╷
│ Error: Invalid provider configuration
│
│ Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration. Add a provider block to the root module and configure the
│ provider's required arguments as described in the provider documentation.
│ Error: configuring Terraform AWS Provider: error validating provider credentials: error calling   sts:GetCallerIdentity: operation error STS: G
etCallerIdentity, https response error StatusCode: 403, RequestID: 594b6dab-e087-4678-8c57-63a65c3d3d41, api error InvalidClientTokenId: The se
curity token included in the request is invalid.
│
│   with provider["registry.terraform.io/hashicorp/aws"],
│   on <empty> line 0:
│ (source code not available)

我期待一个ec2实例被创建,脚本应该运行。

提供商是帮助Terraform与特定云服务交互的插件。在您想通过Terraform使用云服务之前,您必须声明并安装云提供商。请参阅此链接https://developer.hashicorp.com/terraform/language/providers。在您的代码中尝试添加AWS提供商。

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.48.0"
}
}
}
provider "aws" {
# Configuration options
}

然后执行terraform init命令安装提供程序。

相关内容

  • 没有找到相关文章

最新更新