---
# System prepended metadata

title: Untitled

---

### Terraform
* Terraform基本概念介紹
https://hackmd.io/RUyBniujRdaqdrEOM8yYjQ

* Winerve terraform init (用於新環境建立時)
#### gitlab project path : winerve2/terraform/init
```
terraform init -backend-config "bucket=winerve20-tfstate-{stage}" -backend-config "region=ap-southeast-1" -backend-config "key=npi-dashboard-init-ap-southeast-1-{stage}"
```
```
terraform plan -var deploy_stage={stage} -out temp.tfplan
```
```
terraform apply -auto-approve temp.tfplan && rm temp.tfplan
```
```
terraform destroy -var deploy_stage={stage} -auto-approve
```

* Winerve terraform deploy to ecs
#### gitlab project path : winerve2/terraform/
環境變數in gitlab->setting->CI/CD
```
back-end-deploy-dev:
  <<: *only_dev
  stage: dev:deploy
  image:
    name: hashicorp/terraform:${TERRAFORM_VERSION}
    entrypoint: [""]
  script:
    - check_dev_ci_vars
    - cd terraform
    - sed -i "s#__AWS_ACCESS_KEY__#${AWS_ACCESS_KEY_DEV}#" terraform.tfvars
    - sed -i "s#__AWS_SECRET_KEY__#${AWS_SECRET_KEY_DEV}#" terraform.tfvars
    - sed -i "s#__DOCKER_REGISTRY_USERNAME__#${DOCKER_REGISTRY_USERNAME}#" terraform.tfvars
    - sed -i "s/__DOCKER_REGISTRY_PASSWORD__/$(echo $DOCKER_REGISTRY_PASSWORD | sed -e 's/[\/&]/\\&/g')/" terraform.tfvars
    - sed -i "s#__DOCKER_WEB_IMAGE__#$CI_REGISTRY_IMAGE:${CI_COMMIT_SHA:0:8}#" terraform.tfvars
    # fix aa version for save money
    # - sed -i "s#__DOCKER_AA_WORKER_IMAGE__#$CI_REGISTRY_IMAGE/aa_vector:${CI_COMMIT_SHA:0:8}#" terraform.tfvars
    - sed -i "s#__DOCKER_AA_WORKER_IMAGE__#AA_CI_REGISTRY_IMAGE/aa_vector:35a5006a#" terraform.tfvars
    - sed -i "s#__AA_NPI_HOST_IP__#${AA_NPI_HOST_IP_DEV}#" terraform.tfvars
    - sed -i "s#__AA_NPI_HOST_PORT__#${AA_NPI_HOST_PORT_DEV}#" terraform.tfvars
    - sed -i "s#__AA_PREDICT_INTEVAL__#${AA_PREDICT_INTEVAL_DEV}#" terraform.tfvars
    - sed -i "s#__AA_UPDATE_INTERVAL__#${AA_UPDATE_INTERVAL_DEV}#" terraform.tfvars
    - echo ${PROJECT_PROPERTIES_DEV} > project_properties.json
    - cat terraform.tfvars
    - terraform init -backend-config "bucket=winerve20-tfstate-dev" -backend-config "region=ap-southeast-1" -backend-config "key=npi-dashboard-ap-southeast-1-dev-plus"
    - terraform plan -var deploy_stage=dev-plus -out terraform.tfplan
    - terraform apply -auto-approve "terraform.tfplan"
  environment:
    name: dev
  tags:
    - aws
    - m4-large

```


### ECS 參考
* Deploy Web app to ECS
https://hackmd.io/Uffwq5w3QRO3XgujcY-rhA
* Setup an AWS ECS Cluster
https://hackmd.io/0GF3EgQsRL674pGmSp-q9A
