183 lines
4.2 KiB
Markdown
183 lines
4.2 KiB
Markdown
# Terraform OCI - Importação de Recursos
|
|
|
|
## Visão Geral
|
|
|
|
Este documento explica como importar recursos OCI criados manualmente (via console/CLI) para o Terraform.
|
|
|
|
## Por que Importar?
|
|
|
|
Recursos criados fora do Terraform podem ser gerenciados posteriormente com Terraform através da importação.
|
|
|
|
## Como Importar Recursos
|
|
|
|
### 1. Import via CLI
|
|
|
|
```bash
|
|
# Buckets
|
|
terraform import oci_objectstorage_bucket.bucket "compartment-ocid/bucket-name"
|
|
|
|
# API Gateway
|
|
terraform import oci_apigateway_gateway.gateway "gateway-ocid"
|
|
|
|
# API Gateway Deployment
|
|
terraform import oci_apigateway_deployment.deployment "gateway-ocid/deployment-ocid"
|
|
|
|
# VCN
|
|
terraform import oci_core_vcn.vcn "vcn-ocid"
|
|
|
|
# Subnet
|
|
terraform import oci_core_subnet.subnet "subnet-ocid"
|
|
|
|
# OKE Cluster
|
|
terraform import oci_containerengine_cluster.cluster "cluster-ocid"
|
|
|
|
# Load Balancer
|
|
terraform import oci_load_balancer_loadbalancer.lb "lb-ocid"
|
|
```
|
|
|
|
### 2. Estrutura do Código Terraform
|
|
|
|
#### Bucket
|
|
|
|
```hcl
|
|
resource "oci_objectstorage_bucket" "nexus_mfe_user_dev" {
|
|
# ID no formato: compartment-ocid/bucket-name
|
|
bucket_name = "nexus-mfe-user-dev"
|
|
compartment_id = var.compartment_id
|
|
namespace = "grbb7qzeuoag" # namespace OCI
|
|
|
|
storage_tier = "Standard"
|
|
|
|
# metadata = {}
|
|
}
|
|
```
|
|
|
|
#### API Gateway
|
|
|
|
```hcl
|
|
resource "oci_apigateway_gateway" "mfe_user_gateway" {
|
|
compartment_id = var.compartment_id
|
|
endpoint_type = "PUBLIC"
|
|
subnet_id = var.subnet_id
|
|
|
|
display_name = "nexus-mfe-user-gateway"
|
|
|
|
# certificate_id - opcional
|
|
|
|
timeouts {
|
|
create = "30m"
|
|
update = "30m"
|
|
delete = "30m"
|
|
}
|
|
}
|
|
```
|
|
|
|
#### API Gateway Deployment
|
|
|
|
```hcl
|
|
resource "oci_apigateway_deployment" "mfe_user_deployment" {
|
|
gateway_id = oci_apigateway_gateway.mfe_user_gateway.id
|
|
|
|
display_name = "nexus-mfe-user-deployment"
|
|
path_prefix = "/"
|
|
|
|
specification = jsonencode({
|
|
routes = [
|
|
{
|
|
path = "/{req.*}"
|
|
methods = ["GET"]
|
|
backend = {
|
|
type = "HTTP"
|
|
url = "https://namespace.objectstorage.region.oci.customer-oci.com/n/namespace/b/bucket/o"
|
|
}
|
|
}
|
|
]
|
|
})
|
|
}
|
|
```
|
|
|
|
## Pipeline de Import
|
|
|
|
Para importar recursos automaticamente via Azure DevOps:
|
|
|
|
```yaml
|
|
# azure-pipelines-import.yml
|
|
trigger: none
|
|
|
|
parameters:
|
|
- name: resourceType
|
|
type: string
|
|
displayName: Tipo de recurso
|
|
values:
|
|
- bucket
|
|
- api-gateway
|
|
- subnet
|
|
|
|
variables:
|
|
- group: oci-terraform
|
|
|
|
stages:
|
|
- stage: Import_Resource
|
|
jobs:
|
|
- job: Import
|
|
pool:
|
|
vmImage: ubuntu-latest
|
|
steps:
|
|
- task: Terraform@1
|
|
inputs:
|
|
provider: 'oci'
|
|
command: 'custom'
|
|
customCommand: 'import'
|
|
# Resource address no TF
|
|
resourceAddress: 'oci_objectstorage_bucket.bucket_name'
|
|
# ID do recurso no OCI
|
|
resourceId: '$(COMPARTMENT_ID)/bucket-name'
|
|
```
|
|
|
|
## Recursos que Podem Ser Importados
|
|
|
|
### Object Storage
|
|
|
|
| Recurso | Terraform Resource |
|
|
|---------|------------------|
|
|
| Bucket | `oci_objectstorage_bucket` |
|
|
| Object | `oci_objectstorage_object` |
|
|
| Pre-Auth Request | Não suportado (criar via pipeline) |
|
|
|
|
### API Gateway
|
|
|
|
| Recurso | Terraform Resource |
|
|
|---------|------------------|
|
|
| Gateway | `oci_apigateway_gateway` |
|
|
| Deployment | `oci_apigateway_deployment` |
|
|
|
|
### Networking
|
|
|
|
| Recurso | Terraform Resource |
|
|
|---------|------------------|
|
|
| VCN | `oci_core_vcn` |
|
|
| Subnet | `oci_core_subnet` |
|
|
| Security List | `oci_core_security_list` |
|
|
| Route Table | `oci_core_route_table` |
|
|
| Internet Gateway | `oci_core_internet_gateway` |
|
|
| NAT Gateway | `oci_core_nat_gateway` |
|
|
|
|
### Container Engine (OKE)
|
|
|
|
| Recurso | Terraform Resource |
|
|
|---------|------------------|
|
|
| Cluster | `oci_containerengine_cluster` |
|
|
| Node Pool | `oci_containerengine_node_pool` |
|
|
|
|
## Checklist de Importação
|
|
|
|
- [ ] Identificar recursos criados manualmente
|
|
- [ ] Criar código Terraform para o recurso
|
|
- [ ] Executar `terraform import`
|
|
- [ ] Executar `terraform plan` para validar
|
|
- [ ] Commit no repositório Terraform
|
|
|
|
## Referências
|
|
|
|
- [Terraform OCI Provider](https://registry.terraform.io/providers/hashicorp/oci/latest)
|
|
- [OCI CLI Import](https://docs.oracle.com/en-us/iaas/Content/Terraform/tf_import.htm)
|