Automatic deployment ElasticBeanstalk using Bitbucket Pipelines

bitbucket is a service ( Bitbucket Pipelines), which among other things allows extremely simple to automate the deployment of applications in the Amazon cloud, in particular using ElasticBeanstallk. To whom interesting, I ask under kat.



the

Prerequirements


You should already be created ElasticBeanstalk application and deployed environments (in order to do this, refer to the official AWS documentation for, so, for example, unfolds Django ). Since the service is still undergoing testing, you need to request access to it here.

the

project setup


Let's say we have two branches, master and we want to deplot on production environment and development — development environment. Create in the root of the project file bitbucket-pipelines.yml with the following content:

the
image: python:2.7.11

pipelines:
branches:
master:
- step:
script:
- apt-get update # to install required zip
- apt-get install -y zip # required for packaging up the application
- pip install boto3==1.3.0 # required for beanstalk_deploy.py
- zip -rv /tmp/artifact.zip ./* ./.ebextensions ./.elasticbeanstalk # package up the application for deployment
- python beanstalk_deploy.py #  run  the deployment script
development:
- step:
script:
- apt-get update # to install required zip
- apt-get install -y zip # required for packaging up the application
- pip install boto3==1.3.0 # required for beanstalk_deploy.py
- zip -rv /tmp/artifact.zip ./* ./.ebextensions ./.elasticbeanstalk # package up the application for deployment
- python beanstalk_deploy.py # run the deployment script

To control the deployment process, I took the script from the example provided Bitиucket and modified it in order to change the environment depending on the branch.

the
# Copyright 2016 Amazon.com, Inc.  or  its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
# except in compliance with the License. A copy of the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is distributed on an "AS IS"
# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under the License.
"""
Bitbucket is A template for deploying Builds
an application to AWS Elastic Beanstalk
joshcb@amazon.com
v1.0.0
"""
from __future__ import print_function
import os
import sys
from time import strftime, sleep
import boto3
from botocore.exceptions import ClientError

branch = os.getenv('BITBUCKET_BRANCH')
ENVIRONMENT_MAP = {
'master': 'production',
'development': 'development'
}

VERSION_LABEL = strftime("%Y%m%d%H%M%S")
BUCKET_KEY = os.getenv('APPLICATION_NAME') + '/' + VERSION_LABEL + \
'-bitbucket_builds.zip'

def upload_to_s3(artifact):
"""
Uploads an artifact to Amazon S3
"""
try:
client = boto3.client('s3')
except ClientError as err:
print("Failed to create client boto3.\n" + str(err))
return False

try:
client.put_object(
Body=open(artifact, 'rb'),
Bucket=os.getenv('S3_BUCKET'),
Key=BUCKET_KEY
)
except ClientError as err:
print("Failed to upload the artifact to S3.\n" + str(err))
return False
except IOError as err:
print("Failed to access artifact.zip in this directory.\n" + str(err))
return False

return True

def create_new_version():
"""
Creates a new application version in AWS Elastic Beanstalk
"""
try:
client = boto3.client('elasticbeanstalk')
except ClientError as err:
print("Failed to create client boto3.\n" + str(err))
return False

try:
response = client.create_application_version(
ApplicationName=os.getenv('APPLICATION_NAME'),
VersionLabel=VERSION_LABEL,
Description='a New build from Bitbucket',
SourceBundle={
'S3Bucket': os.getenv('S3_BUCKET'),
'S3Key': BUCKET_KEY
},
Process=True
)
except ClientError as err:
print("Failed to create application version.\n" + str(err))
return False

try:
if response['ResponseMetadata']['HTTPStatusCode'] is 200:
return True
else:
print(response)
return False
except (KeyError, TypeError) as err:
print(str(err))
return False

def deploy_new_version():
"""
Deploy a new version to AWS Elastic Beanstalk
"""
try:
client = boto3.client('elasticbeanstalk')
except ClientError as err:
print("Failed to create client boto3.\n" + str(err))
return False

try:
response = client.update_environment(
ApplicationName=os.getenv('APPLICATION_NAME'),
EnvironmentName=ENVIRONMENT_MAP.get(branch)
VersionLabel=VERSION_LABEL,
)
except ClientError as err:
print("Failed to update environment.\n" + str(err))
return False

print(response)
return True

def main():
"Your favorite wrapper''s favorite wrapper."
if not upload_to_s3('/tmp/artifact.zip'):
sys.exit(1)
if not create_new_version():
sys.exit(1)
# Wait for the new version to be consistent before deploying
sleep(5)
if not deploy_new_version():
sys.exit(1)

if __name__ == "__main__":
main()

the

setup Bitbucket


Create the necessary environment variables:



S3_BUCKET indicate the one that is used to deploy the application in the region and account. Then get working pipelines:
image
Article based on information from habrahabr.ru

Comments

  1. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.
    Data Science Training in Indira nagar
    Data Science Training in btm layout
    Data Science Training in Kalyan nagar
    Data Science training in Indira nagar
    Data Science Training in Marathahalli

    ReplyDelete
  2. Irrespective of rankings, this will help in huge traffic generation, on your website, over time and, in turn,will steadily increase the number of potential customers for your products and services.


    web designing training in chennai

    web designing training in velachery

    digital marketing training in chennai

    digital marketing training in velachery

    rpa training in chennai

    rpa training in velachery

    tally training in chennai

    tally training in velachery

    ReplyDelete

Post a Comment

Popular posts from this blog

Powershell and Cyrillic in the console (updated)

Active/Passive PostgreSQL Cluster, using Pacemaker, Corosync