Deploy to AWS with Elastic Beanstalk
Asked by Bailey Davis:

Hi all,

Am trying to get my head around deploying apps into AWS Beanstalk using BitBucket Piplines.

End goal will be deploying some Django apps.

Anyways, I have been giving it a go and following the tutorial linked below:

<https://support.atlassian.com/bitbucket-cloud/docs/deploy-to-aws-with-elastic-beanstalk/>

I am having issues where its failing after deployment saying:  

*? Deployment failed. Environment "Cvsstaging-env" is Red.*   

And in AWS:

|------------------------------|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 2021-09-11 15:29:53 UTC+0930 | WARN | Environment health has transitioned from Degraded to Severe. 100.0 % of the requests are failing with HTTP 5xx. ELB processes are not healthy on all instances. ELB health is failing or not available for all instances. Impaired services on all instances. One or more TargetGroups associated with the environment are in a reduced health state: - awseb-AWSEB-1KNZ27HWP6PPX - Warning |
| 2021-09-11 15:28:53 UTC+0930 | WARN | Environment health has transitioned from Severe to Degraded. 100.0 % of the requests are failing with HTTP 5xx. Application update completed 66 seconds ago and took 14 seconds. Impaired services on all instances. One or more TargetGroups associated with the environment are in a reduced health state: - awseb-AWSEB-1KNZ27HWP6PPX - Warning                                          |

I assumed this was likely due to an issue with my app, so insead of trying to deploy my own app, i decided to just try deploy the app linked in the tutorial itself:  
<https://bitbucket.org/bitbucketpipelines/example-aws-elasticbeanstalk-deploy/src/master/>  

But even this repository with the only changes being, changing it to Python 3.8 in the docker file as my Beanstalk is running Python 3.8, and obviously AWS details and credentials, but even this is having the same error.  

I feel like im missing something super basic?


Answers:
author: Igor Stoyanov
body: [@Bailey Davis](/t5/user/viewprofilepage/user-id/4407632) [@Denis Kisina](/t5/user/viewprofilepage/user-id/4649575) hi. After investigation i found that problem is with wrong Elastic Beanstalk configuration. I also have this errors: *Deployment failed. Environment "your-env" is Red.*

In logs i found the reason: ModuleNotFoundError: No module named 'application'

This two articles helped me a lot:

<https://stackoverflow.com/questions/33467817/how-to-debug-elasticbeanstalk-error-x-of-the-requests-are-failing-with-http-5x>

<https://stackoverflow.com/questions/68365462/modulenotfounderror-no-module-named-application-error-in-aws>

After this i tried to do this as [aws tutorial](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-flask.html) suggests and everything worked correctly.

Folder structure:

```
Your app

 - application.py
 - requirements.txt
 - bitbucket-pipelines.yml
```

application.py:

```
from flask import Flask

# print a nice greeting.
def say_hello(username = "World"):
    return '<p>Hello %s!</p>\n' % username

# some bits of text for the page.
header_text = '''
    <html>\n<head> <title>EB Flask Test</title> </head>\n<body>'''
instructions = '''
    <p><em>Hint</em>: This is a RESTful web service! Append a username
    to the URL (for example: <code>/Thelonious</code>) to say hello to
    someone specific.</p>\n'''
home_link = '<p><a href="/">Back</a></p>\n'
footer_text = '</body>\n</html>'

# EB looks for an 'application' callable by default.
application = Flask(__name__)

# add a rule for the index page.
application.add_url_rule('/', 'index', (lambda: header_text +
    say_hello() + instructions + footer_text))

# add a rule when the page is accessed with a name appended to the site
# URL.
application.add_url_rule('/<username>', 'hello', (lambda username:
    header_text + say_hello(username) + home_link + footer_text))

# run the app.
if __name__ == "__main__":
    # Setting debug to True enables debug output. This line should be
    # removed before deploying a production app.
    application.debug = True
    application.run()
```

requirements.txt:

```
click==8.0.3
Flask==1.1.2
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
Werkzeug==2.0.2
```

bitbucket-pipelines.yml:

```
image: atlassian/default-image:2

pipelines:
  default:
    - step:
        name: "Build and Test"
        script:
          - echo "Everything is awesome!"
          - zip application.zip requirements.txt application.py
          - pipe: atlassian/aws-elasticbeanstalk-deploy:0.2.3
            variables:
              AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
              AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
              AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
              APPLICATION_NAME: 'application-test'
              COMMAND: 'upload-only'
              ZIP_FILE: 'application.zip'
              S3_BUCKET: 'test-elasticbeanstalk'
              VERSION_LABEL: 'deploy-$BITBUCKET_BUILD_NUMBER-multiple'
    - step:
        name: "Deploy to Staging"
        deployment: staging
        script:
        - echo "Deployment!"
        - pipe: atlassian/aws-elasticbeanstalk-deploy:0.2.3
          variables:
            AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
            AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
            AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
            APPLICATION_NAME: 'application-test'
            COMMAND: 'deploy-only'
            VERSION_LABEL: 'deploy-$BITBUCKET_BUILD_NUMBER-multiple'
            ENVIRONMENT_NAME: 'test-staging2'
            WAIT: 'true'
```

Elastic Beanstalk configuration:

Platform - Python 3.8 running on 64bit Amazon Linux 2

Tier name - WebServer

![Screenshot 2021-12-08 at 11.55.04.png](https://community.atlassian.com/t5/image/serverpage/image-id/172815i346EAD5E3DA5CAE5/image-size/large?v=v2&px=999 "Screenshot 2021-12-08 at 11.55.04.png")

Pipelines result:

![Screenshot 2021-12-08 at 11.57.32.png](https://community.atlassian.com/t5/image/serverpage/image-id/172817i7476897031759A49/image-size/large?v=v2&px=999 "Screenshot 2021-12-08 at 11.57.32.png")

Hope this helps. Regards, Igor.

comments: author: Igor Stoyanov
body: Added [Tutorial for Django application](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html)


author: Denis Kisina
body: [@Bailey Davis](/t5/user/viewprofilepage/user-id/4407632) Did you get a solution to this?

comments: author: Igor Stoyanov
body: [@Denis Kisina](/t5/user/viewprofilepage/user-id/4649575) Hi. We will investigate this issue and notify you about the results.  

Regards, Igor


author: Igor Stoyanov
body: [@Bailey Davis](/t5/user/viewprofilepage/user-id/4407632) Hi. Thanks for your question. Please, add your pipeline configuration and logs to help you.  

Regards, Igor.


Tags: aws

beanstalk

bitbucket-pipelines

cloud

django

python