Extending Nginx Configuration on AWS Elastic Beanstalk and Integrating with AWS CodeBuild

0

By default, the Nginx server provided by Elastic Beanstalk responds with a 413 Request Entity Too Large error when the body of an HTTP request exceeds 1MB. This can pose problems, especially when applications need to process files or images that surpass this size limit. In this article, I'll outline the steps to extend the Nginx configuration, and integrating this configuration into a CI/CD pipeline using AWS CodeBuild.

Local Configuration

To configure Nginx locally, we first need to create a folder structure. In the root directory of our project, we create a folder named .platform/nginx/conf.d. Within this folder, we create a file named client_max_body_size.conf with the desired configuration, such as client_max_body_size 50M; for a maximum body size of 50MB. This value must be adapted according to your requirements.

.platform/nginx/conf.d/client_max_body_size.conf

The folder structure would then look like this:

├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
├── .platform
│   └── nginx
│       └── conf.d
│           └── client_max_body_size.conf
└── pom.xml

Integration into the Build Process

To ensure that the configured file is included in the built application, it needs to be included in the resulting archive (e.g., a ZIP file). This can be achieved by adjusting the build script or configuration, depending on the tools and frameworks used.

A POM file would then be extended like this:

<!-- pom.xml -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
  <executions>
    <execution>
      <id>prepare</id>
      <phase>package</phase>
      <configuration>
        <tasks>
          <copy todir="${project.build.directory}/${project.build.finalName}/"overwrite="false">
            <fileset dir=""includes=".platform/**"/>
            <fileset dir="${project.build.directory}"includes="*.jar"/>
          </copy>
          <zip destfile="${project.build.directory}/${project.build.finalName}.zip"
            basedir="${project.build.directory}/${project.build.finalName}"/>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

AWS Elastic Beanstalk Deployment

Now that the application has been configured, it can be built, and the resulting zip file can be uploaded and deployed through the Elastic Beanstalk user interface. During deployment, the Nginx server will automatically adopt the customized settings, ensuring seamless integration with the application.

Enter image description here

Integration with AWS CodeBuild CI/CD Pipeline

If we're using a CI/CD pipeline for automated deployment of our application, we need to ensure that the Nginx configuration is also integrated into the build process. To do this, we create a new file named Procfile in the root directory of our project and add the following content:

web: java -jar target/YourApplicationName.jar

The folder structure would then look like this:

├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
├── .platform
│   └── nginx
│       └── conf.d
│           └── client_max_body_size.conf
└── pom.xml
├── Procfile

Replace YourApplicationName with the name of our application. The name can be retrieved from the previously created zip file, for example:

Enter image description here

To ensure that the Procfile is included in the build process, we modify our previously created Maven plugin:

<!-- pom.xml -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
  <executions>
    <execution>
      <id>prepare</id>
      <phase>package</phase>
      <configuration>
        <tasks>
          <copy todir="${project.build.directory}/${project.build.finalName}/"overwrite="false">
            <fileset dir=""includes=".platform/**"/>
            <fileset dir="" includes="Procfile"/>
            <fileset dir="${project.build.directory}"includes="*.jar"/>
          </copy>
          <zip destfile="${project.build.directory}/${project.build.finalName}.zip"
            basedir="${project.build.directory}/${project.build.finalName}"/>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

After completing these steps, all necessary adjustments on our Spring side are completed. Now, let's move to our AWS CodeBuild build project. In the edit area, we need to edit the BuildSpec and adjust the artifacts section as follows:

# buildspec.yml (AWS CodeBuild)
artifacts:
  files:
    - target/YourApplicationName.jar
    - .ebextensions//*
    - .platform/**/*
    - Buildfile
    - Procfile
  discard-paths: no

Replace YourApplicationName with the application name specified in the Procfile. In the AWS CodeBuild interface, it will appear as follows:

Enter image description here

Now, you can save your changes and initiate the deployment process. Upon doing so, you will observe that the configuration of the Nginx server has been successfully applied.

Conclusion

In conclusion, configuring the Nginx server for applications hosted on AWS Elastic Beanstalk is crucial for overcoming default error limitations such as the 413 Request Entity Too Large. By following the outlined steps, developers can successfully configure the Nginx server to optimize application functionality.

I trust that this article will assist fellow developers in addressing similar challenges, and I am open to feedback and contributions to further enhance this topic.

  • Thanks for great article!

profile picture
asked 2 months ago115 views
No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions