DEV Community

Cover image for Laravel Vapor Multiple Deploy File to different AWS account or region
Norby Baruani
Norby Baruani

Posted on

2 1

Laravel Vapor Multiple Deploy File to different AWS account or region

Brief Description

Will be sharing how I was able to deploy same codebase to different AWS account on Vapor using multiple vapor.yml config file with the help of bash scripts.

At the time of writing this, Laravel Vapor did not support deploy command to receive a filepath to deploy against. Hopefully one day that feature can be added by the team.

These approach allows you to use separate AWS account for your staging and production environments if needed as well better than that deploy to multiple aws account or multiple aws region.

How it works

Assuming you already have your laravel project setup and working with Laravel Vapor.

  1. Rename vapor.yml to vapor.{account}.yml, project should be a unique name different to the other file you need to create. eg. vapor.dev.yml 2.Create another vapor config file to deploy to the different account or region. eg. vapor.devcon.yml and make sure config are correct.
  2. Make sure vapor.yml file doesn't exist on your root directory of project anymore.
  3. Copy the following scripts to the root directory of project and make sure to make them executable.
    #!/usr/bin/env bash
    set -E
    trap 'throw_exception' ERR
    if [[ -z "${1}" ]]; then
    consolelog "usage: vapor.sh {account} {command}" "error"
    consolelog "example: vapor.sh account env:pull staging"
    exit
    fi
    VAPOR_PROJECT_FILE="vapor.$1.yml"
    if test -f "$VAPOR_PROJECT_FILE"; then
    echo "Using file $VAPOR_PROJECT_FILE"
    else
    consolelog "file $VAPOR_PROJECT_FILE does not exist." "error"
    exit
    fi
    VAPOR_FILE=vapor.yml
    if test -f "$VAPOR_FILE"; then
    consolelog "$VAPOR_FILE file exist, we are not allowed to run the vapor script" "error"
    exit
    fi
    # Move the file to a temporary "vapor.yml" which the vapor bin can use
    cp $VAPOR_PROJECT_FILE $VAPOR_FILE
    # Forward all arguments except the first one to the vapor binary
    vapor ${@:2}
    rm -f $VAPOR_FILE
    view raw vapor-cli.sh hosted with ❤ by GitHub
    #!/usr/bin/env bash
    set -E
    trap 'throw_exception' ERR
    if [[ -z "${1}" ]] || [[ -z "${2}" ]]; then
    consolelog "usage: deploy.sh {account} {environment}" "error"
    consolelog "example: deploy.sh account staging"
    exit
    fi
    VAPOR_PROJECT_FILE="vapor.$1.yml"
    if test -f "$VAPOR_PROJECT_FILE"; then
    consolelog "Using file $VAPOR_PROJECT_FILE"
    else
    consolelog "file $VAPOR_PROJECT_FILE does not exist." "error"
    exit
    fi
    VAPOR_FILE=vapor.yml
    if test -f "$VAPOR_FILE"; then
    consolelog "$VAPOR_FILE file exist, we are not allowed to run the vapor script" "error"
    exit
    fi
    # Move the file to a temporary "vapor.yml" which the vapor bin can use
    cp $VAPOR_PROJECT_FILE $VAPOR_FILE
    vapor deploy $2 --commit="$(git show -s --format=%h)" --message="$(git show -s --format=%s)"
    rm -f $VAPOR_FILE
    view raw vapor-deploy.sh hosted with ❤ by GitHub

Interact with scripts

At this stage you should have 2 vapor config file on your root directory. From our example you would have:

vapor.dev.yml
vapor.devcon.yml
Enter fullscreen mode Exit fullscreen mode
Vapor CLI:
./vapor-cli.sh dev env:pull staging
./vapor-cli.sh dev env:push staging
Enter fullscreen mode Exit fullscreen mode
Deploy:
./vapor-deploy.sh dev staging
./vapor-deploy.sh devcon production
Enter fullscreen mode Exit fullscreen mode

Take Away

This approach can be used to deploy Staging and Production to different AWS account or Region as well.

All that will be

  • To have correct file naming and config to have only correct environment setting on the related file.

  • To make changes to both script by to only allow 1 argument and use 1st argument as filename and environment to run against.

Eg.

vapor.staging.yml
vapor.production.yml
Enter fullscreen mode Exit fullscreen mode
./vapor-deploy.sh staging
./vapor-deploy.sh production
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs