Skip to content

Instantly share code, notes, and snippets.

@jonathan-kosgei
Last active December 19, 2018 17:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathan-kosgei/6ed94e6bc365a54932fdadff3e9d02cc to your computer and use it in GitHub Desktop.
Save jonathan-kosgei/6ed94e6bc365a54932fdadff3e9d02cc to your computer and use it in GitHub Desktop.
Deploy PHP site to AWS Elasticbeanstalk with Bitbucket Pipelines

ElasticBeanstalk on AWS

Getting Started

To deploy a new Magento store automatically via Bitbucket CI. Follow the following steps.

1. Set the following environment variables for the bitbucket repo with the magento store source code

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-west-2 #or any aws region you'd like to run your app in
AWS_EB_PLATFORM=php-5.6 # this needs to be set to 5.6, which supports both Magento 1.x and 2.x sites
DB_USERNAME= # MySQL Database username
DB_PASSWORD= # MySQL Database password
DB_SIZE= # MySQL Database size, from 5GB-1024GB

To do this, got to Settings -> Pipelines -> Environment Variables Be sure to check the Secure variable setting for the AWS credentials. This will stop Bitbucket from printing the keys anywhere in cleartext.

2. Copy the above bitbucket-pipelines.yml file to the root of the repo with the Magento source code.

3. Git push your code!

That's all you need to do!

To watch the build run, and to get the url at which the new Magento store will become available, go to Pipelines and click on the pipeline with your commit.

The RDS Database details will be available to your app via the following environment variables;

RDS_HOSTNAME – The hostname of the DB instance.
Amazon RDS console label – Endpoint is the hostname.
RDS_PORT – The port on which the DB instance accepts connections. The default value varies between DB engines.
Amazon RDS console label – Port
RDS_DB_NAME – The database name, ebdb.
Amazon RDS console label – DB Name
RDS_USERNAME – The user name that you configured for your database.
Amazon RDS console label – Username
RDS_PASSWORD – The password that you configured for your database.
# set aws env key variables
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# AWS_DEFAULT_REGION=us-west-2
# AWS_EB_PLATFORM=php-5.4
# DB_USERNAME=
# DB_PASSWORD=
# DB_SIZE=
# Repo names must contain only letters, digits, and the dash character and may not start or end with a dash
image: jkosgei/eb-ci-lite
pipelines:
default:
- step:
script:
- BITBUCKET_REPO_SLUG=`echo $BITBUCKET_REPO_SLUG | tr -d "_"`
- eb init $BITBUCKET_REPO_SLUG -r $AWS_DEFAULT_REGION -p $AWS_EB_PLATFORM
- envs=`eb list | tr -d '* ' |tr '\n' ' '`
- function list_include_item { local list="$1"; local item="$2"; if [[ $list =~ (^|[[:space:]])"$item"($|[[:space:]]) ]] ; then result=0; else result=1; fi; return $result;}; `list_include_item "$envs" "$BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH"` && check="yes" || check="no"
- echo $envs $check
- if [[ "$check" == "no" ]]; then echo "Creating..."; eb create --database.engine mysql --database.username $DB_USERNAME --database.password $DB_PASSWORD --database.size $DB_SIZE $BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH -r $AWS_DEFAULT_REGION -c $BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH -p $AWS_EB_PLATFORM --elb-type classic; else echo "Deploying..."; eb deploy $BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH -r $AWS_DEFAULT_REGION; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment