How can I dynamically pass lambda version number in circleci job?

Hello,

I’m trying to dynamically pass the latest version of my lambda function to update an alias for my function. However circleci keeps throwing errors when it gets to that section of the job run.

When I passed the command like so - aws lambda update-alias --function-version $LATEST_VERSION --name Production --function-name s3_lambda_fucntion, the error below comes up.

Parameter validation failed:
Invalid length for parameter FunctionVersion, value: 0, valid min length: 1
Exited with code exit status 252

And when I passed the command like so - aws lambda update-alias --function-version “$LATEST_VERSION” --name Production --function-name s3_lambda_fucntion, the error below comes up.

aws: error: argument --function-version: expected one argument
Exited with code exit status 252

Here’s some workflow snippet from my config.

jobs:

collator_build:

    # build steps #

collator_deploy_production:
   executor: lambda-executor
   steps:
    - shallow-checkout/checkout
    - install_aws_cli 
    - run:
        name: Get Latest Lambda Version
        command: |
          LATEST_VERSION=$(aws lambda list-versions-by-function --function-name s3_lambda_fucntion --query "Versions[-1].Version" --output text)
          echo "$LATEST_VERSION" 
             
    - run:
        name: Update lambda alias
        command: |
              aws lambda update-alias --function-version $LATEST_VERSION --name Production --function- name s3_lambda_fucntion

workflows:
    jobs:
      - collator_build
      - collator_deploy_production

Does anyone know how I can work around this or maybe I’m missing something? Thanks.
Side Note: I tried to rerun my job from the failed workflow using SSH and had no problems running the steps to up get the latest version and update the alias from the circleci box.

Each run command is executed in its own shell instance so the first run defines the environment variable, which is then lost when the run command completes.

To work around this CircleCI provides $BASH_ENV, which allows environment variables to be exported from one shell and then reloaded into any additional shell created.

For the example you have provided it maybe easier to just combine the 2 run commands into a single step.

Hi,

Thanks for your response. Using $BASH_ENV to pass the variable solved my issue.