How to reference template files in config.yml

Hello Everyone. I have created an AWS infrastructure template. This template, when run will create an s3 bucket and other resources. I have stored the template.yml file in my project’s GitHub repository, the same location as the circleci config.yml file(https://github.com/ndakena/Give-your-app-supper-powers/edit/master/.circleci/). I have gone ahead to create a job in the config.yml file to run the template

   # AWS infrastructure
create_infrastructure:
      docker:
          - image: amazon/aws-cli
      steps:
        - checkout
        - run:
            name: Ensure backend infrastructure exist
            command: |
              aws cloudformation deploy \
                --template-file template.yml \
                --stack-name my-stack

But the job fails with the error “Invalid template path template.yml”
Question:

  • What is the valid file path in this case
  • Can someone tell me the right thing to do? I am a beginner
1 Like

That looks right. I have some pipelines that do similar things. I’d recommend adding a diagnostic step in your job like the following, just to nail down the state of your workspace.

- run:
    name: Diags
    command: |
      pwd
      ls -la . ..
      printenv
1 Like

Hello @klsetzer, thanks for your reply. I did what you recommended but the same error. Then I decided to use --template-file = template.yml \ instead of --template-file template.yml \ and the error disappeared. But I now have a different error, see the screenshot below.

Any thought?

What was the output from the diagnostic step I suggested? The idea was to look at the output to make sure your template.yml was where you thought it was.

In any case, I can see from the output you pasted that you have an equals sign between your --template-file parameter and the name of the template file. The AWS CLI doesn’t use a equals sign for the --template-file parameter. Try taking that out.

If it still doesn’t work, look at the output from the diagnostic step to see if you see anything suspicious. You’re welcome to paste it here, though you may want to remove anything that’s sensitive.

1 Like

Hello, @klsetzer thanks for your prompt reply.
I have tried as you suggested but no luck! I removed the equal sign, the print of the diagnostic step is as below

#!/bin/bash -eo pipefail
pwd
ls -la . ..
printenv
/root/project
.:
total 236
drwxr-xr-x 8 root root   4096 Jul 10 01:34 .
dr-xr-x--- 3 root root   4096 Jul 10 01:34 ..
drwxr-xr-x 4 root root   4096 Jul 10 01:34 .circleci
drwxr-xr-x 4 root root   4096 Jul 10 01:34 .git
-rw-r--r-- 1 root root      0 Jul 10 01:34 .gitignore
-rw-r--r-- 1 root root   3044 Jul 10 01:34 LICENSE.md
-rw-r--r-- 1 root root  20989 Jul 10 01:34 README.md
drwxr-xr-x 4 root root   4096 Jul 10 01:34 backend
drwxr-xr-x 4 root root   4096 Jul 10 01:34 frontend
-rw-r--r-- 1 root root   3686 Jul 10 01:34 package.json
drwxr-xr-x 2 root root   4096 Jul 10 01:34 starter
-rw-r--r-- 1 root root    470 Jul 10 01:34 tempate.yml
-rw-r--r-- 1 root root 169479 Jul 10 01:34 udapeople-pipeline.png
drwxr-xr-x 2 root root   4096 Jul 10 01:34 util

..:
total 12
dr-xr-x---  3 root root 4096 Jul 10 01:34 .
drwxr-xr-x 36 root root 4096 Jul 10 01:34 ..
drwxr-xr-x  8 root root 4096 Jul 10 01:34 project
CIRCLE_WORKFLOW_JOB_ID=1dd9ecbf-0447-43a3-98c5-bec0117ac4cb
CIRCLE_BUILD_NUM=228
HOSTNAME=8220da0d3c83
CIRCLE_NODE_TOTAL=1
TYPEORM_HOST=***********
TYPEORM_MIGRATIONS=*********************
CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS=
CIRCLE_INTERNAL_SCRATCH=/tmp/circleci-100289353
CIRCLE_PROJECT_USERNAME=ndakena
CIRCLE_INTERNAL_TASK_DATA=/.circleci-task-data
NO_PROXY=127.0.0.1,localhost,circleci-internal-outer-build-agent
CIRCLE_NODE_INDEX=0
CIRCLE_COMPARE_URL=
TYPEORM_CONNECTION=********
CIRCLE_BRANCH=master
SSH_AUTH_SOCK=/tmp/circleci-100289353/ssh_auth_sock
CIRCLE_JOB=create_infrastructure
SLACK_WEBHOOK=*******************************************************************************
CIRCLE_WORKING_DIRECTORY=~/project
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TYPEORM_ENTITIES=***********************************
CIRCLE_WORKFLOW_WORKSPACE_ID=9941aa04-be01-49c6-b841-12b851b68d30
CIRCLE_USERNAME=ndakena
CIRCLE_PREVIOUS_BUILD_NUM=227
AWS_DEFAULT_REGION=*********
PWD=/root/project
CIRCLE_STAGE=create_infrastructure
AWS_SECRET_ACCESS_KEY=****************************************
CIRCLE_BUILD_URL=https://circleci.com/gh/ndakena/Give-your-app-supper-powers/228
CIRCLE_WORKFLOW_ID=9941aa04-be01-49c6-b841-12b851b68d30
CIRCLECI=true
AWS_ACCESS_KEY_ID=********************
SHLVL=1
HOME=/root
CIRCLE_SHA1=b2bb41fd71a19751fde859f33820b6741a732779
CI=true
CIRCLE_REPOSITORY_URL=git@github.com:ndakena/Give-your-app-supper-powers.git
CIRCLE_INTERNAL_CONFIG=/.circleci-runner-config.json
TYPEORM_PORT=****
BASH_ENV=/tmp/.bash_env-5f07c588c759e051fe50799e-0-build
TYPEORM_USERNAME=***********
CIRCLE_SHELL_ENV=/tmp/.bash_env-5f07c588c759e051fe50799e-0-build
TYPEORM_PASSWORD=**********
TYPEORM_DATABASE=***********
TYPEORM_MIGRATIONS_DIR=****************
CIRCLE_PROJECT_REPONAME=Give-your-app-supper-powers
_=/usr/bin/printenv
CircleCI received exit code 0

Any thoughts from it? Below is my full job definition

    # AWS infrastructure
    create_infrastructure:
        docker:
            - image: amazon/aws-cli
        working_directory: /root/project
        steps:
          - checkout
          - run:
              name: Diags
              command: |
                pwd
                ls -la . ..
                printenv
          - run:
              name: Ensure backend infrastructure exist
              command: |
                aws cloudformation deploy \
                --template-file /root/project/template.yml \
                --stack-name my-stack

Removing/Adding the working_directory didn’t help! Changing to ~/project or ~/root … did not help either.
Please I am really stuck here

Ok, I put together a working sample for you.

Here’s the public CircleCI Project: LiquidChicken

Here’s the public GitHub repo: circleci-simple-aws-demo

Hello @klsetzer, thanks a million!
The link to the public CircleCI Project seems to be broken?

I don’t know why the CircleCI Project is not public. Ignore it. Everything you need is in the GitHub repo.

Okay, thanks a lot. I’m try it.

I corrected the link to the CircleCI Project.

Here it is, for simplicity: https://circleci.com/gh/liquidchicken/circleci-simple-aws-demo

1 Like

Yes, the link works

You’ll also need to set environment variables for your project with your AWS credentials. This is what it should look like:

Yes, I did that already.

Hello @klsetzer, I can’t imagine that it failed with the same error message.

What do you mean?

Can you post your .circleci/config.yml?

Here is it

version: 2.1
orbs:
    node: circleci/node@1.1.6
    aws-cli: circleci/aws-cli@1.1.0
    slack: circleci/slack@3.4.2
jobs:
    # The build phase
    frontend-build-phase:
      executor:
        name: node/default
        tag: '13.8.0'
      steps:
          - checkout
          - run: cd frontend && npm i && npm run build
    backend-build-phase:
      executor:
        name: node/default
        tag: '13.8.0'
      steps:
          - checkout
          - run: cd backend && npm i && npm run build
    # The test Phase
    frontend-test-phase:
      executor:
        name: node/default
        tag: '13.8.0'
      steps:
          - checkout
          - run: cd frontend && npm install --save oauth-sign && npm run test
    backend-test-phase:
          executor:
              name: node/default
              tag: '13.8.0'
          steps:
              - checkout
              - run: cd backend && npm i && npm run test
    # Analyze phase
    backend-analyze-phase:
          executor:
              name: node/default
              tag: '13.8.0'
          steps:
              - checkout
              - run: cd backend && npm i && npm audit fix --audit-level=critical --force
    frontend-analyze-phase:
          executor:
              name: node/default
              tag: '13.8.0'
          steps:
              - checkout
              - run: cd frontend && npm i && npm audit --audit-level=critical --force

    # AWS infrastructure
    create_infrastructure:
          executor: aws-cli/default
          steps:
              - checkout
              - run:
                 name: Diags
                 command: |
                    pwd
                    ls -la . ..
              - aws-cli/setup
              - run:
                  name: Verify AWS CLI
                  command: aws sts get-caller-identity
              - run:
                  name: Ensure backend infrastructure exist
                  command: |
                    aws cloudformation deploy \
                    --template-file template.yml \
                    --stack-name my-stack   
              - run:
                  name: Test the things
                  command: exit 0
              - slack/status:
                 channel: $SLACK_CHANNEL
                 failure_message: "Failure: build [$CIRCLE_BUILD_NUM]($CIRCLE_BUILD_URL) of [<<pipeline.git.branch>>](<<pipeline.project.git_url>>/tree/<<pipeline.git.branch>>) _($CIRCLE_USERNAME)_"
                 success_message: "Success: build [$CIRCLE_BUILD_NUM]($CIRCLE_BUILD_URL) of [<<pipeline.git.branch>>](<<pipeline.project.git_url>>/tree/<<pipeline.git.branch>>) _($CIRCLE_USERNAME)_"

    print_env:
       docker:
           - image: cimg/base:2020.01
       steps:
        - checkout
        - run: 
            name: "echo an env var that is part of our project"
            command: |
              echo $AWS_DEFAULT_REGION # this env var must be swt within the project
 ## Workflows for pipelines     
workflows:
    All-pipeline-phases:
        jobs:   
            - print_env
            - create_infrastructure:
                filters:
                   branches:
                     only:
                       - master
            # Testing Jobs
            - backend-test-phase:
                requires:
                   - create_infrastructure
            - frontend-test-phase:
                requires:
                   - backend-test-phase
            # Building Jobs
            - backend-build-phase:
                requires:
                  - backend-test-phase
                  - frontend-test-phase
            - frontend-build-phase:
                requires:
                  - frontend-test-phase
            # Analyzing jobs
            - backend-analyze-phase:
                 requires:
                   - backend-build-phase
            - frontend-analyze-phase:
                 requires:
                   - frontend-build-phase
```

Your template.yml is not in the current directory for some reason. Based on what you’ve posted in this thread, I don’t see how. Replace your create_infrastructure job with this, then paste the result here.

jobs:
  create_infrastructure:
    executor: aws-cli/default
    steps:
      - checkout
      - aws-cli/setup
      - run:
          name: Ensure backend infrastructure exist
          command: |
            pwd
            ls -la
            aws cloudformation deploy \
              --template-file template.yml \
              --stack-name my-stack
1 Like

Hello @klsetzer , Sorry for taking too long, I was trying to complete an online quiz.

Here is the results

#!/bin/bash -eo pipefail
pwd
ls -la
aws cloudformation deploy \
  --template-file template.yml \
  --stack-name my-stack
/home/circleci/project
total 236
drwxr-xr-x  8 circleci circleci   4096 Jul 10 04:27 .
drwxr-xr-x 13 circleci circleci   4096 Jul 10 04:27 ..
drwxr-xr-x  4 circleci circleci   4096 Jul 10 04:27 backend
drwxr-xr-x  4 circleci circleci   4096 Jul 10 04:27 .circleci
drwxr-xr-x  4 circleci circleci   4096 Jul 10 04:27 frontend
drwxr-xr-x  8 circleci circleci   4096 Jul 10 04:27 .git
-rw-r--r--  1 circleci circleci      0 Jul 10 04:27 .gitignore
-rw-r--r--  1 circleci circleci   3044 Jul 10 04:27 LICENSE.md
-rw-r--r--  1 circleci circleci   3686 Jul 10 04:27 package.json
-rw-r--r--  1 circleci circleci  20989 Jul 10 04:27 README.md
drwxr-xr-x  2 circleci circleci   4096 Jul 10 04:27 starter
-rw-r--r--  1 circleci circleci    470 Jul 10 04:27 tempate.yml
-rw-r--r--  1 circleci circleci 169479 Jul 10 04:27 udapeople-pipeline.png
drwxr-xr-x  2 circleci circleci   4096 Jul 10 04:27 util

Invalid template path template.yml

Exited with code exit status 255
CircleCI received exit code 255