1 什么是 Terraform?
Terraform 是由 HashiCorp 创建的"基础架构即代码"(Infrastructure-as-Code,IaC)开源工具。Terraform 的配置语言是 HashiCorp Configuration Language(HCL),用来替代更加冗长的 JSON 和 XML 等配置语言。Terraform通过tf配置文件管理Aws云资源(ec2,s3,lambda等各种资源)。
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的权限策略
点击下一步,再点击创建访问秘钥
访问秘钥access_key,秘密访问秘钥secret_key
- 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