amazon-web-servicesaws-lambdaterraformterraform-provider-awsterragrunt

automating terraform import using github actions


I am importing an aws lambda function created manually, now in to terraform. Since we have many env and regions we use terragrunt. I was able to import the lambda function using module as:

resource "aws_lambda_function" "imported_lambda" {
   function_name = "myLambda"
}

Then I did terragrunt import with:

- name: Terragrunt import
  id: terragrunt_import
  uses: gruntwork-io/terragrunt-action@v2
  with:
    tf_version: ${{ github.event.inputs.tf_version }}
    tg_command: 'import'
    tg_version: ${{ github.event.inputs.tg_version }}
    tg_dir: target-repo
  env:
    AWS_SDK_LOAD_CONFIG: 1

My plan for next step was to do a terragrunt show as json and extract that json into a file. Then write a python script to read that json file and create a new module to manage this lambda function with terraform.

If I do: terraform show -json > myfile.txt using below step:

- name: Terragrunt show
  uses: gruntwork-io/terragrunt-action@v2
  with:
    tf_version: ${{ github.event.inputs.tf_version }}
    tg_command: 'show -json > myfile.txt'
    tg_version: ${{ github.event.inputs.tg_version }}
    tg_dir: target-repo
  env:
    AWS_SDK_LOAD_CONFIG: 1

It throws error that too many arguments.

Is there a way to extract all the imported resource (lambda) into a text file so that I read it using python?


Solution

  • I'd try using show -json as the command and then referencing the action's tg_action_output output from a following step, either writing it to a file or using it as is.

    Something like:

    - name: Terragrunt show
      id: terragrunt_show
      uses: gruntwork-io/terragrunt-action@v2
      with:
        tf_version: ${{ github.event.inputs.tf_version }}
        tg_command: 'show -json'
        tg_version: ${{ github.event.inputs.tg_version }}
        tg_dir: target-repo
      env:
        AWS_SDK_LOAD_CONFIG: 1
    
    - run: 'echo ${{ steps.terragrunt_show.outputs.tg_action_output }}'