1 什么是 Terraform?
Terraform 是由 HashiCorp 创建的"基础架构即代码"(Infrastructure-as-Code,IaC)开源工具。Terraform 的配置语言是 HashiCorp Configuration Language(HCL),用来替代更加冗长的 JSON 和 XML 等配置语言。Terraform通过tf配置文件管理Aws云资源(ec2,s3,lambda等各种资源)。
![](https://file.jishuzhan.net/article/1793488227483521026/9bd8915d65964351ea82b48b181f2f3b.webp)
2 EC2 Linux安装Terraform
bash
$ sudo yum install -y yum-utils shadow-utils
$ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
$ sudo yum -y install terraform
$ terraform -version
Terraform v1.8.3
on linux_amd64
3 Terraform
- 创建秘钥
s3_user有s3的权限策略
![](https://file.jishuzhan.net/article/1793488227483521026/02eed1134de91c55ac2abe6420a172d4.webp)
点击下一步,再点击创建访问秘钥
![](https://file.jishuzhan.net/article/1793488227483521026/47b6d45821ef1e8bd2128c3b14868fda.webp)
访问秘钥access_key,秘密访问秘钥secret_key
![](https://file.jishuzhan.net/article/1793488227483521026/d17a93d79063250ad27b6666bd2cc69c.webp)
- tf文件
bash
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-2"
access_key = "AKIAVRUVxxxxxxxxxx"
secret_key = "S1sU31BBxxxxxxxxxxxxxxxxxxxxxxxx"
}
resource "aws_s3_bucket" "test" {
bucket = "tf-bucket"
}
官方文档:Terraform Registry
- 执行
bash
$ terraform init
$ terraform plan
$ terraform apply
生成是tf-bucket的s3的bucket