클라우드/DevOps

terraform tutorial & vpc-ec2구성하기

dayeonsheep 2024. 7. 8. 13:04

 

테라폼 다운받고

 

Quick start tutorial

Now that you've installed Terraform, you can provision an NGINX server in less than a minute using Docker on Mac, Windows, or Linux. You can also follow the rest of this tutorial in your web browser.

Click on the tab(s) below relevant to y

our operating system.

Docker Desktop for MacDocker Desktop for WindowsDocker Engine for LinuxWeb Browser

Download Docker Desktop for Mac.

After you install Terraform and Docker on your local machine, start Docker Desktop.

$ open -a Docker
 

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.

Mac or LinuxWindows
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 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
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
425d5ee58619        e791337790a6              "nginx -g 'daemon of…"   20 seconds ago      Up 19 seconds       0.0.0.0:8000->80/tcp     tutorial
 

To stop the container, run terraform destroy.

$ terraform destroy
 

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

 

 

 

https://github.com/stacksimplify/hashicorp-certified-terraform-associate

 

GitHub - stacksimplify/hashicorp-certified-terraform-associate: Hashicorp Certified Terraform Associate

Hashicorp Certified Terraform Associate. Contribute to stacksimplify/hashicorp-certified-terraform-associate development by creating an account on GitHub.

github.com

 

https://velog.io/@stitcj/CloudFormation-Terraform

 

CloudFormation, Terraform

2022.11.01.

velog.io

 

 

##2

 

Clone example configuration

Clone the example repository for this tutorial.

$ git clone https://github.com/hashicorp/learn-terraform-aws-cloud-control.git
 

Change to the repository directory.

$ cd learn-terraform-aws-cloud-control
 

This configuration defines a KMS key managed by the traditional AWS provider. You will use this key to encrypt your Cassandra table.

 

 

Create KMS key

Initialize this configuration.

$ terraform init
Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Installing hashicorp/aws v3.59.0...
- Installed hashicorp/aws v3.59.0 (signed by HashiCorp)

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
 

Apply the configuration to create your KMS key. Respond to the confirmation prompt with a yes.

$ terraform apply
## ...
Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + aws_region = "us-west-2"
  + kms_key_id = (known after apply)

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_kms_key.terraform: Creating...
aws_kms_key.terraform: Still creating... [10s elapsed]
aws_kms_key.terraform: Still creating... [20s elapsed]
aws_kms_key.terraform: Creation complete after 22s [id=33198581-e648-46a3-b78d-1eb2edf9ab94]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

aws_region = "us-west-2"
kms_key_id = "33198581-e648-46a3-b78d-1eb2edf9ab94"
 

Add AWS Cloud Control provider

The traditional AWS provider does not currently support Amazon Keyspaces, but the Cloud Control provider does. Add the Cloud Control provider to your configuration so you can use Terraform to manage a Cassandra keyspace and table.

First, update the terraform block in main.tf to add the Cloud Control and random providers. You will use the random provider to generate a random keyspace name.

main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
    awscc = {
      source  = "hashicorp/awscc"
      version = "~> 0.1.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.1.0"
    }
  }
}
 

Next, add provider blocks for both the Cloud Control and random providers. Configure the Cloud Control provider to use the same region as the traditional AWS provider.

main.tf

provider "awscc" {
  region = var.aws_region
}

provider "random" {}
 

Reinitialize your configuration to install the new providers.

$ terraform init
Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/awscc...
- Reusing previous version of hashicorp/random from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Installing hashicorp/random v3.1.0...
- Installed hashicorp/random v3.1.0 (signed by HashiCorp)
- Using previously-installed hashicorp/aws v3.59.0
 

Now that you have installed the Cloud Control provider, you can create your Cassandra resources.

 

 

https://isc9511.tistory.com/166

 

[AWS - Terraform] Network Setting (VPC, Subnet, IGW, NGW, Routing table, Security Group)

* 참조 사항 - 필자는 학습을 목적으로 main.tf에 전체 인프라 구축 코드를 작성하였으며, 이에 대해 각기 설명함 - .tf 파일을 resource 별로 분할할 시, 필자와 코드가 다를 수 있음 - Terraform 기초부터

isc9511.tistory.com

 

 

리팩토링 하기!!!

https://honglab.tistory.com/120

이렇게도 구성할 수 있긴 한데 이건 좀 micro 하고

msa같은... 구조에서는 너무 복잡하기 때문에 나눌 필요가 있을지 모르나

개인적인 구성으로는 이건 too micro하다

 

https://custom-li.tistory.com/214

 

효율적인 인프라 관리를 위한 Terraform 모듈 입문하기

개요 Terraform과 같은 Infrastructure as Code(IaC) 도구를 사용한다면, 대규모 인프라를 효츌적으로 관리할 수 있다. 그러나, 단일 Terraform 프로젝트로 대규모 인프라를 관리하게 된다면, 많은 리소스들이

custom-li.tistory.com

 

 

--

 vpc 와 ec2 연결 구성하면서 겪었던 에러인데

나 혼자 했으면 ssh관해서 뭘 해야하나 했을 텐데 바로 알려주셔서 금방 해결했다

 

https://github.com/dayeon1201/dy-tf

 

GitHub - dayeon1201/dy-tf: building infra via terraform

building infra via terraform . Contribute to dayeon1201/dy-tf development by creating an account on GitHub.

github.com

1차 수정 버전

2차 리팩토링은 아직 로컬에만 있도다...