Gitlab Ci Java Build and Upload to S3

Past Ram Dileepan, Sr. Solutions Builder at AWS
By Santiago Cardenas, Sr. Solutions Architect, Serverless at AWS

In lodge to deliver serverless applications, customers oft plough to DevOps principles to efficiently build, deploy, operate, and iterate on features and changes.

Continuous integration and continuous deployment (CI/CD) is 1 of the major components of DevOps that helps deliver code faster and more reliably to production.

The continuous integration offering from GitLab, an AWS Partner Network (APN) Advanced Technology Partner with the AWS DevOps Competency, provides a rich set up of features for automating how new code is incorporated into your software and how new versions of your software get congenital and deployed.

In this mail service, y'all volition larn how to create a sample serverless application using AWS Serverless Application Model (SAM) and how GitLab continuous integration helps with building, testing, and deploying the application in your Amazon Web Services (AWS) account.

About AWS Serverless Awarding Model

A serverless awarding is comprised of multiple resources, which tin include functions, event sources, datastores, and other resource that work together to perform tasks.

Serverless applications are more than than only AWS Lambda functions—they can include additional resources, such equally APIs, databases, and consequence source mappings.

AWS Serverless Application Model is an open up source framework for building serverless applications. SAM includes the AWS SAM Template Specification and AWS SAM Command Line Interface (CLI).

The AWS SAM Template Specification is an extension of AWS CloudFormation used to define your serverless awarding resource as a template. It provides a more convenient and concise syntax to express functions, APIs, databases, and event source mapping when compared to typical CloudFormation template.

The CLI enables y'all to build, test, and deploy serverless applications that are defined using a SAM template.

Getting Started

To get started, ensure you accept your ain GitLab account and AWS business relationship. This post assumes you have required permissions to configure Gitlab CI/CD pipelines and create AWS resource in your account.

Nosotros volition create a sample serverless awarding using the SAM CLI and deploy it to an AWS account using GitLab CI/CD pipeline. Nosotros'll use GitLab every bit the repository for our source code.

When a programmer checks in their new lawmaking, the GitLab continuous integration fetches the source code from the repository, and then build, package, and deploy to the AWS account.

These are the steps in this procedure:

  1. Install and configure the SAM CLI.
  2. Create an AWS serverless application using SAM CLI.
  3. Craft the .gitlab-ci.yml file.
  4. Ready your AWS credentials in your GitLab account.
  5. Test locally.
  6. Deploy your awarding.
  7. Exam your deployment.

Step ane: Install and Configure the SAM CLI

Some steps in this documentation apply the SAM CLI. Please follow the instructions on installing SAM CLI.

If you use AWS Cloud9 as your integrated development surroundings (IDE), the AWS Command Line Interface (CLI), SAM CLI, Docker, and necessary Docker images are installed for you.

Create an Amazon Simple Storage Service (Amazon S3) bucket in your AWS account to store the built package for deployment. Provide required permissions to the user you will be using in later steps to deploy the SAM application to write to this saucepan.

Step 2: Creating an AWS SAM Awarding Using SAM CLI

You lot can create a serverless application by defining all required resources in an AWS SAM template. AWS SAM also provides some quick-start templates to create an application. In this post, we'll use 1 of those templates to create a sample application.

Before you create a SAM awarding, create a new GitLab project and clone it to your local surroundings. To create a SAM app from the CLI, open a terminal, change to the newly cloned project, and enter the following text:

sam init -r python3.8 -n gitlab-case --app-template "howdy-world"

This creates a SAM app named gitlab-case using the default configuration, a single Python 3.eight function invoked by an Amazon API Gateway endpoint.

To see boosted runtimes supported by SAM and options for sam init, enter sam init -h.

Git push button the application back to the GitLab project.

Footstep three: Crafting the .gitlab-ci.yml File

GitLab CI/CD pipelines are configured using a YAML file called .gitlab-ci.yml inside each project. This defines the structure and order of the pipelines.

In a.gitlab-ci.yml file in the root of your project, place the following code and supervene upon the #S3Bucket# with the Amazon S3 bucket name you created to shop the package:

Let's examine the config file more closely:

  • The Docker paradigm to utilize for this build is specified and pulled from the Docker hub. The image with Python 3.8 is used in this case since the sample application is written in Python 3.8.
  • AWS Command Line Interface and AWS SAM CLI are installed in the script section.
  • SAM builds, packages, and deploys the application.

As function of the CI/CD process, we recommend you lot browse your code for quality and vulnerabilities in bundled libraries. You tin find these security offerings from our AWS Lambda Applied science Partners.

Step iv: Setting Upwardly Your AWS Credentials with GitLab

To interact with your AWS account, the GitLab CI/CD pipelines require both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to be defined in your GitLab projection settings. Yous can do this underSettings > CI/CD > Variables. For more information, please see this GitLab ReadMe.

The AWS credentials yous provide must include AWS Identity and Access Management (IAM) policies that provision right access control to AWS Lambda, Amazon API Gateway, AWS CloudFormation, and IAM resources.

Step 5: Testing Locally

SAM provides functionality to exam your applications locally. As we mentioned earlier, you lot must have SAM CLI installed locally for you to test locally.

SAM provides a default issue inevents/event.json that includes a message body of {\"message\": \"hello earth\"}. If you lot laissez passer that outcome into the HelloWorldFunction, it should respond with the same body:

sam local invoke HelloWorldFunction -east events/event.json

Output should be:

{"message": "hello world"}

After you lot confirm the Lambda function is working as expected, you lot can test the API Gateway using the following steps.

Get-go get-go the API locally:

sam local start-api

SAM over again launches a Docker container, this fourth dimension with a mocked API Gateway listening on localhost:3000. At present, you tin can call the hello API as follows:

curl http://127.0.0.ane:3000/hello

Output again should be:

{"message": "hello world"}

Footstep half-dozen: Deploying Your Application

Git push the changes to your GitLab repository, and the GitLab pipeline volition automatically deploy your application. If your build and deploy are successful, go to the adjacent step to test your deployed awarding.

If your build fails, please take a look at the build log to see why it failed. Some common reasons are:

  • Incompatible versions of software (for example, the Python runtime version may be different from the Python on the build motorcar). Address this past installing the proper versions of the software. If your build machine has an incompatible version of the software, install the proper version via the .gitlab-ci.yml file.
  • Yous may not be able to access your AWS account from GitLab. Check the environment variables you configured with AWS credentials nether the CI/CD settings in GitLab.
  • You may non take permission to deploy a serverless awarding. Brand sure you lot provide all required permissions to deploy a serverless application. Your credentials must have permissions to the S3 saucepan, in addition to being able to create the necessary resources of the sample SAM app.

Pace seven: Testing Your Application

To test the application you deployed, become to the build log and click on Show Consummate Raw in the upper right corner.

GitLab-CI-CD-1.1

Look for the HelloWorldApi – API Gateway endpoint similar to shown below.

GitLab-CI-CD-2.1

Finally, employ whorl to test the API:

curl https://<api-id>.execute-api.us-eastward-ane.amazonaws.com/Prod/hello/

Output should be:

{"bulletin": "hello globe"}

Conclusion

In this mail service, you used GitLab continuous integration and AWS Serverless Application Model to create, build, test, and deploy a serverless application. With GitLab, users can build and deploy serverless applications with minimal configuration to accept advantage of continuous integration for serverless application development.

Larn more than almost GitLab and AWS Serverless Application Model (SAM).

.
GitLab-APN-Blog-CTA-1
.


GitLab – APN Partner Spotlight

GitLab is an AWS DevOps Competency Partner. Its continuous integration offer provides a rich prepare of features for automating how new lawmaking is incorporated into your software and how new versions of your software get built and deployed.

Contact GitLab | Solution Overview | AWS Marketplace

*Already worked with GitLab? Rate this Partner

*To review an APN Partner, you must exist an AWS customer that has worked with them directly on a project.

molinaliek1947.blogspot.com

Source: https://aws.amazon.com/blogs/apn/using-gitlab-ci-cd-pipeline-to-deploy-aws-sam-applications/

0 Response to "Gitlab Ci Java Build and Upload to S3"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel