Vzzarr
Vzzarr

Reputation: 5700

How to import an already existing AWS Glue Job in Terraform?

I deployed my Glue Jobs via Console and / or boto3 library until now.

I want to move to Infrastructure as Code using Terraform and manage all the Glue resources with this tool. Now to import already existing resources I'm aware of the command terraform import ADDR ID but I'm not sure how does that work.

Anyone can explain me how the import works with AWS Glue?

Upvotes: 3

Views: 3508

Answers (1)

Vzzarr
Vzzarr

Reputation: 5700

Referencing https://www.terraform.io/docs/cli/import/usage.html before importing an AWS Glue Job it is necessary to create a .tf file in the current working directory in your CLI.

So let's create a main.tf file like this:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.27"
    }
  }
}

provider "aws" {
  region = "my-region-1"
  assume_role {
    role_arn = "arn:aws:iam::012345678901:role/MyRole"
  }
}

resource "aws_glue_job" "my_job_resource" {
    name     = "my-glue-job"
    role_arn = "arn:aws:iam::012345678901:role/MyGlueJobRole"
    command {
        name            = "glueetl"
        script_location = "s3://my-bucket/my-script.py"
        python_version  = "3"
    }
}

Where:

  • my-region-1 is the AWS region where operating
  • arn:aws:iam::012345678901:role/MyRole is the AWS Role assumed when running Terraform commands (which is what will access / interact with AWS Resources)
  • my-glue-job is the name of the existing Glue Job
  • arn:aws:iam::012345678901:role/MyGlueJobRole is the AWS Role of the existing Glue Job
  • s3://my-bucket/my-script.py is the S3 location of the Glue Job script

The command field is specific of the Glue Job import as considered a mandatory field, for other fields references and / or variations: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/glue_job

Once the main.tf file is ready, run from the CLI in the same folder containing the file the following commands:

terraform init
terraform import aws_glue_job.my_job_resource my-glue-job
terraform show

Few details on these commands:

  1. it will provision the folder with the necessary Terraform plug-ins necessary to interact with AWS
  2. my_job_resource is the name of the block that identifies AWS resource while my-glue-job is the actual Glue Job name (in both cases refer to the main.tf file)
  3. it will prompt the entire structure imported by the Glue Job.

Copy and replace the output generated from the last step into the main.tf file, replacing the block resource "aws_glue_job" "my_job_resource".

It is now imported in a Terraform script.

The same principle can be reused for any other Glue resources, just reference the documentation page https://registry.terraform.io/providers/hashicorp/aws/latest/docs

Upvotes: 2

Related Questions