0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

docker localstack, terraformを利用して s3のlocal環境を作成する

Posted at

docker localstack, terraformを利用して s3のlocal環境を作成する

aws-cliのimageにterraformコマンドを利用できる環境を作成するからのはs

table of contents

  1. docker 環境を作成する
  2. main.tfを作成する
  3. apply!

1. docker環境を作成する

compose.yaml
services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
    build:
      context: ./localstack
      dockerfile: dockerfile
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
    environment:
      # LocalStack configuration: https://docs.localstack.cloud/references/configuration/
      - DEBUG=${DEBUG:-0}
      - SERVICES=s3
    volumes:
      - "localstack_volume:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
  terraform:
    container_name: terraform_container
    build:
      context: ./terraform
      dockerfile: dockerfile
      target: development
      args:
        TERRAFORM_VERSION: "1.10.3"
        AWS_CLI_VERSION: "2.22.27"
    tty: true
    volumes:
      - ./terraform:/usr/terraform

volumes:
  localstack_volume: {}

FROM localstack/localstack
ARG AWS_CLI_VERSION

FROM amazon/aws-cli:${AWS_CLI_VERSION} as development

ARG TERRAFORM_VERSION

WORKDIR /usr/terraform

RUN yum install unzip -y

RUN curl https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -o terraform_${TERRAFORM_VERSION}_linux_amd64.zip &&\
    unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip && rm terraform_${TERRAFORM_VERSION}_linux_amd64.zip &&\
    mv terraform /usr/bin/terraform

ENTRYPOINT [ "/bin/bash" ]

2. main.tfを作成する

terraform {
  backend "local" {}
}

provider "aws" {
  region     = "ap-northeast-1"
  access_key = "access_key"
  secret_key = "secret_key"

  s3_use_path_style           = true
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints {
    s3 = "http://localstack-main:4566"
  }
}

resource "aws_s3_bucket" "application" {
  bucket = "csharp"

  tags = {
    Name        = "My bucket"
    Environment = "Dev"
  }
}

3. apply!!

$ docker exec -it XXX bash
$ terraform init -y
$ terraform plan 
$ terraform apply 

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_s3_bucket.application will be created
  + resource "aws_s3_bucket" "application" {
      + acceleration_status         = (known after apply)
      + acl                         = (known after apply)
      + arn                         = (known after apply)
      + bucket                      = "csharp"
      + bucket_domain_name          = (known after apply)
      + bucket_prefix               = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + object_lock_enabled         = (known after apply)
      + policy                      = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags                        = {
          + "Environment" = "Dev"
          + "Name"        = "My bucket"
        }
      + tags_all                    = {
          + "Environment" = "Dev"
          + "Name"        = "My bucket"
        }
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + cors_rule (known after apply)

      + grant (known after apply)

      + lifecycle_rule (known after apply)

      + logging (known after apply)

      + object_lock_configuration (known after apply)

      + replication_configuration (known after apply)

      + server_side_encryption_configuration (known after apply)

      + versioning (known after apply)

      + website (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

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_s3_bucket.application: Creating...
aws_s3_bucket.application: Creation complete after 1s [id=csharp]

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?