1
votes

How to deploy Java Maven based application on AWS ElasticBeanstalk using Bitbucket pipelines? I found there example how to do it using python script. I modify yml and py script for my needs. All steps are going ok, bot on last one(execute python script it throws error. What is going wrong? Thanks.

YML File

image: openkbs/jre-mvn-py3

pipelines:
 branches:
  master:
   - step:
      caches:
        - maven
        - node
      script:
        - mvn clean install
        - apt-get update
        - apt-get -y install python-pip
        - pip install boto3
        - export AWS_SECRET_ACCESS_KEY=KEY
        - export AWS_ACCESS_KEY_ID=KEY
        - export AWS_DEFAULT_REGION=REGION
        - python beanstalk_deploy.py

Python Script Source

Traceback (most recent call last): File "beanstalk_deploy.py", line 107, in main()

File "beanstalk_deploy.py", line 99, in main if not create_new_version():

File "beanstalk_deploy.py", line 56, in create_new_version Process=True

File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 314, in _api_call return self._make_api_call(operation_name, kwargs)

File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 586, in _make_api_call api_params, operation_model, context=request_context)

File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 641, in _convert_to_request_dict api_params, operation_model)

File "/usr/local/lib/python2.7/dist-packages/botocore/validate.py", line 291, in serialize_to_request raise ParamValidationError(report=report.generate_report()) botocore.exceptions.ParamValidationError: Parameter validation failed:

Invalid type for parameter SourceBundle.S3Bucket, value: None, type: , valid types:

1

1 Answers

0
votes

Already found a solution. First of all we create new one image with all tools in it.

Modify yml pipeline file

image: hardtyz/jre-mvn-py3

pipelines:
  branches:
    master:
      - step:
          caches:
            - maven
            - node
          script:
            - mvn clean install
            - export AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY
            - export AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY_ID
            - export AWS_DEFAULT_REGION=AWS_DEFAULT_REGION
            - python3 beanstalk_deploy.py

And modify python script. Just update variables in head of script with your values.

from __future__ import print_function
import sys
from time import strftime, sleep
import boto3
from botocore.exceptions import ClientError

VERSION_LABEL = strftime("%Y%m%d%H%M%S")
BUCKET_KEY = VERSION_LABEL + '-bitbucket_builds.jar'
BUCKET_NAME = 'your_bucket_name'
APP_NAME='AppName'
ENV_NAME='AppName-env'
ARTIFACT_PATH='./core/target/core-0.0.1-SNAPSHOT.jar'

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

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

return True

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 boto3 client.\n" + str(err))
    return False

try:
    response = client.update_environment(
        ApplicationName=APP_NAME,
        EnvironmentName=ENV_NAME,
        VersionLabel=VERSION_LABEL,
    )
except ClientError as err:
    print("Failed to update environment.\n" + str(err))
    return False

print(response)
return True

def main():
if not upload_to_s3(ARTIFACT_PATH):
    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()