首页 > 其他分享 >Terraform

Terraform

时间:2024-06-13 14:25:39浏览次数:9  
标签:instance aws Terraform terraform docker type

Terraform

1 Install Terraform

1.1 Amazon Linux

Install yum-config-manager to manage your repositories.

$ sudo yum install -y yum-utils

Use yum-config-manager to add the official HashiCorp Linux repository.

$ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo

Install Terraform from the new repository.

$ sudo yum -y install terraform

1.2 Verify the installation

[root@ip-172-31-24-108 ~]# terraform -version
Terraform v1.8.5
on linux_amd64

1.3 Enable tab completion

bash

touch ~/.bashrc

Then install the autocomplete package.

terraform -install-autocomplete

2 Quick start tutorial

2.1 install Docker Engine

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl start docker
sudo docker run hello-world

2.2

Create a directory named learn-terraform-docker-container.

$ mkdir learn-terraform-docker-container

This working directory houses the configuration files that you write to describe the infrastructure you want Terraform to create and manage. When you initialize and apply the configuration here, Terraform uses this directory to store required plugins, modules (pre-written configurations), and information about the real infrastructure it created.

Navigate into the working directory.

$ cd learn-terraform-docker-container

In the working directory, create a file called main.tf and paste the following Terraform configuration into it.

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0.1"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.image_id
  name  = "tutorial"

  ports {
    internal = 80
    external = 8000
  }
}

Initialize the project, which downloads a plugin called a provider that lets Terraform interact with Docker.

$ terraform init

Provision the NGINX server container with apply. When Terraform asks you to confirm type yes and press ENTER.

$ terraform plan
$ terraform apply

Verify the existence of the NGINX container by visiting localhost:8000 in your web browser or running docker ps to see the container.

$ docker ps

To stop the container, run terraform destroy.

$ terraform destroy

You've now provisioned and destroyed an NGINX webserver with Terraform.

3 build

3.1 Prerequisites

To follow this tutorial you will need:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
# aws --version
aws-cli/2.16.7 Python/3.11.8 Linux/6.1.92-99.174.amzn2023.x86_64 exe/x86_64.amzn.2023

To use your IAM credentials to authenticate the Terraform AWS provider, set the AWS_ACCESS_KEY_ID environment variable.

$ export AWS_ACCESS_KEY_ID=

Now, set your secret key.

$ export AWS_SECRET_ACCESS_KEY=

3.2 Write configuration

Each Terraform configuration must be in its own working directory. Create a directory for your configuration.

$ mkdir learn-terraform-aws-instance

Change into the directory.

$ cd learn-terraform-aws-instance

Create a file to define your infrastructure.

$ touch main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-east-1"
}

resource "aws_instance" "app_server-1" {
  ami               		= "ami-08a0d1e16fc3f61ea"
  instance_type     		= "t2.micro"
  availability_zone 		= "us-east-1b"
  vpc_security_group_ids     = ["sg-052590e460117f9ad"]
  key_name                   = "key-0505485572be88125"

  tags = {
    Name = "ansible-mem-1"
  }
}
resource "aws_instance" "app_server-2" {
  ami               		= "ami-08a0d1e16fc3f61ea"
  instance_type     		= "t2.micro"
  availability_zone 		= "us-east-1b"
  vpc_security_group_ids     = ["sg-052590e460117f9ad"]
  key_name                   = "key-0505485572be88125"

  tags = {
    Name = "ansible-mem-2"
  }
}

3.3 Initialize the directory

When you create a new configuration — or check out an existing configuration from version control — you need to initialize the directory with terraform init.

$ terraform init

3.4 Format and validate the configuration

We recommend using consistent formatting in all of your configuration files. The terraform fmt command automatically updates configurations in the current directory for readability and consistency.

$ terraform fmt
$ terraform validate
Success! The configuration is valid.

3.5 Create infrastructure

$ terraform plan
$ terraform apply

3.6 Inspect state

When you applied your configuration, Terraform wrote data into a file called terraform.tfstate

$ terraform show

3.7 Manually Managing State

Terraform has a built-in command called terraform state for advanced state management. Use the list subcommand to list of the resources in your project's state.

$ terraform state list
aws_instance.app_server

4 Change infrastructure

4.1 Configuration

修改main.tf

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

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-east-1"
}

resource "aws_instance" "app_server-1" {
  ami               		= "ami-08a0d1e16fc3f61ea"
  instance_type     		= "t2.micro"
  availability_zone 		= "us-east-1b"
  vpc_security_group_ids     = ["sg-052590e460117f9ad"]
  + key_name                   = "Ansible"

  tags = {
    Name = "ansible-mem-1"
  }
}
resource "aws_instance" "app_server-2" {
  ami               		= "ami-08a0d1e16fc3f61ea"
  instance_type     		= "t2.micro"
  availability_zone 		= "us-east-1b"
  vpc_security_group_ids     = ["sg-052590e460117f9ad"]
  + key_name                   = "Ansible"

  tags = {
    Name = "ansible-mem-2"
  }
}

4.2 Apply Changes

$ terraform apply

5 Destroy infrastructure

$ terraform destroy

6 Define input variables

6.1 Set the instance name with a variable

Create a new file called variables.tf with a block defining a new instance_name variable in the learn-terraform-aws-instance directory.

$ touch variables.tf
variable "instance_name" {
  description = "Value of the Name tag for the EC2 instance"
  type        = string
  default     = "Ansible_mem"
}

variable "instance_ami" {
  description = "Value of the ami for the EC2 instance"
  type        = string
  default     = "ami-08a0d1e16fc3f61ea"
}

variable "instance_type" {
  description = "Value of the type for the EC2 instance"
  type        = string
  default     = "t2.micro"
}

variable "availability_zone" {
  description = "Value of the AZ for the EC2 instance"
  type        = string
  default     = "us-east-1b"
}

variable "vpc_security_group_ids" {
  description = "Value of the SG for the EC2 instance"
  type        = set(string)
  default     = ["sg-052590e460117f9ad"]
}

variable "key_name" {
  description = "Value of the SG for the EC2 instance"
  type        = string
  default     = "Ansible"
}

标签:instance,aws,Terraform,terraform,docker,type
From: https://www.cnblogs.com/goldtree358/p/18245763

相关文章

  • 普鲁米预览失败:出现 1 个错误:* 配置 Terraform AWS 提供程序:未找到 Terraform AWS 提
    我遇到了这样一个问题:当我尝试运行命令将资源导入现有的pulumi堆栈时,预览总是失败错误如下类型名称计划信息pulumi:pulumi:堆栈dev1错误-aws:ebs:Volumeidimport1错误诊断:pulumi:pulumi:Stack(dev):error:预览失败:出现1个错误:*配置TerraformAWS提......
  • Terraform管理OpenStack
      官方安装指南https://developer.hashicorp.com/terraform/installhttps://developer.hashicorp.com/terraform/intro/getting-started/install.html安装sudoyuminstall-yyum-utilssudoyum-config-manager--add-repohttps://rpm.releases.hashicorp.com/RHEL/h......
  • 一键云部署:ROS的Terraform托管服务助你轻松上线2048经典游戏
    在现代云计算环境中,自动化部署已经成为一项重要的任务。Terraform,作为HashiCorp公司的一款开源工具,以其强大的基础设施即代码(IaC)能力,使得我们能够轻松管理和部署各种云资源。阿里云资源编排服务(ResourceOrchestrationService,ROS)是一种简单易用的云计算资源自动化部署服务,提供了......
  • 云原生周刊:Terraform 1.8 发布 | 2024.5.6
    开源项目推荐xlskubectl用于控制Kubernetes集群的电子表格。xlskubectl将GoogleSpreadsheet与Kubernetes集成。你可以通过用于跟踪费用的同一电子表格来管理集群。git-syncgit-sync是一个简单的命令,它将git存储库拉入本地目录,等待一段时间,然后重复。当远程存储库......
  • IaC 管理新思路:Walrus 和 Terraform 的差异化探索
    Terraform的社区版本及商业化版本,让其成为在基础设施即代码(IaC)领域中可靠的部署和管理平台。尽管目前TerraformCloud/Enterprise仍然是最为广泛采用的IaC管理解决方案,但它存在一定的局限性。 随着用户需求和偏好的变化,以及鉴于成本考虑、灵活性需求以及简化复杂性的紧迫......
  • OpenTofu路在何方:定量分析Terraform issue数据,洞察用户需求|OpenTofu Day 闪电演讲
    数澈软件Seal首席架构师李平辉提交的演讲议题“AliasTerraform=Tofu.Job'sDone,NowWhat?”入选KubeConEU同场活动OpenTofuDay,本文为演讲实录。  大家好,我是Lawrence,是Seal的首席架构师。今天将由我为大家带来LighteningTalk。在Seal,我们研发了一款开源软......
  • 从零开始的terraform之旅 - 3命令部分- 部署基础架构 (plan apply destroy)
    3命令部分-部署基础架构(planapply)文章目录3命令部分-部署基础架构(planapply)部署基础架构planplanningmodes**Refresh-onlymode**仅刷新模式,非常有用PlanningOptions规划选项apply命令Plan**Options**apply选项destroy命令部署基础架构terraform的......
  • Terraform小知识-字符串拼接
    在Terraform中,可以使用字符串插值来将多个字符串拼接在一起。字符串插值使用${}语法,其中包含要插入的表达式或变量名。使用字符串插值可以将变量的值动态地插入到字符串中,从而构建一个完整的字符串。例如,假设我们有两个变量name和region,我们想要创建一个AWSS3存......
  • terraform小知识 - 如何取消掉环境变量中设置的身份凭证.md
    terraform小知识-如何取消掉环境变量中设置的身份凭证.md文章目录terraform小知识-如何取消掉环境变量中设置的身份凭证.md需求测试总结需求目前通过环境变量配置了aws的身份凭证,如果直接执行terraform命令,则会调用该身份,但是直接unset取消环境变量又会影响同......
  • 使用 Amazon Bedrock 上的 Claude 3 将架构图转换为 CDK/Terraform 代码
    概述在云原生领域,基础设施即代码(IaC)对于开发人员和DevOps团队来说是一种不可避免的实践。最近,AmazonBedrock上线了Claude3Sonnet模型和这个模型的图像转文本能力。这无疑开启了一个新时代,也就是实现架构图与IaC工具的无缝融合,如亚马逊云科技云开发工具包(CDK)或......