You are on page 1of 44

Desarrollo de Aplicaciones en la Nube

Infrastructure As A Service

Desarrollo de Aplicaciones
en la Nube
Infrastructure As A Service

José M. Álvarez – University of Málaga

M.U. Ingeniería Informática E.T.S.I. Informática 1


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Table of contents
• Amazon Web Services
• Elastic Cloud Computing
• Simple Storage Server
• Code Star
• CodeBuild
• Code Deploy
• Amazon SDK for Java
• Amazon SDK for Python

M.U. Ingeniería Informática E.T.S.I. Informática 2


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

AWS Build
• AWS CodeBuild is a fully managed build service in the cloud. AWS CodeBuild
compiles your source code, runs unit tests, and produces artifacts that are ready
to deploy.
• WS CodeBuild eliminates the need to provision, manage, and scale your own
build servers.
• It provides prepackaged build environments for the most popular programming
languages and build tools such as Apache Maven, Gradle, and more.

M.U. Ingeniería Informática E.T.S.I. Informática 3


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

How to Run AWS CodeBuild

M.U. Ingeniería Informática E.T.S.I. Informática 4


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

How to Run AWS CodeBuild


1. You can run AWS CodeBuild by using the AWS CodeBuild or AWS CodePipeline
console.
2. You can also automate the running of AWS CodeBuild by using the AWS
Command Line Interface (AWS CLI) or the AWS SDKs.
3. You can add AWS CodeBuild as a build or test action to the build or test stage
of a pipeline in AWS CodePipeline. AWS CodePipeline is a continuous delivery
service that enables you to model, visualize, and automate the steps required
to release your code.

M.U. Ingeniería Informática E.T.S.I. Informática 5


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

How AWS CodeBuild Works

M.U. Ingeniería Informática E.T.S.I. Informática 6


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

How AWS CodeBuild Works


1. As input, you must provide AWS CodeBuild with a build project.
2. AWS CodeBuild uses the build project to create the build environment.
3. AWS CodeBuild downloads the source code into the build environment and then uses
the build specification (build spec), as defined in the build project or included directly in
the source code.
4. If there is any build output, the build environment uploads its output to an Amazon S3
bucket.
5. While the build is running, the build environment sends information to AWS CodeBuild
and Amazon CloudWatch Logs.
6. While the build is running, you can use the AWS CodeBuild console, AWS CLI, or AWS
SDKs, to get summarized build information from AWS CodeBuild and detailed build
information from Amazon CloudWatch Logs.
M.U. Ingeniería Informática E.T.S.I. Informática 7
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

AWS CodeBuild Concepts

• A build project defines how AWS CodeBuild runs a build. It includes information such
as where to get the source code, the build environment to use, the build commands
to run, and where to store the build output.
• A build environment represents a combination of operating system, programming
language runtime, and tools that AWS CodeBuild uses to run a build.
• A build spec is a collection of build commands and related settings, in YAML format,
that AWS CodeBuild uses to run a build.
• A build represents a set of actions performed by AWS CodeBuild to create output
artifacts (for example, a JAR file) based on a set of input artifacts (for example, a
collection of Java class files).
M.U. Ingeniería Informática E.T.S.I. Informática 8
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Plan a Build for AWS CodeBuild

Before you run your build with AWS CodeBuild, you must answer these questions:
1. Where is the source code located? AWS CodeBuild currently supports building
from the following source code repository providers. The source code must
contain a build specification (build spec) file, or the build spec must be
declared as part of a build project definition.

M.U. Ingeniería Informática E.T.S.I. Informática 9


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Plan a Build for AWS CodeBuild


Repository provider Required

AWS CodeCommit Repository name.


(Optional) Commit ID associated with the source code.

Input bucket name.


Object name corresponding to the build input ZIP file that contains the
Amazon S3 source code.
(Optional) Version ID associated with the build input ZIP file.

GitHub Repository name.


(Optional) Commit ID associated with the source code.

Bitbucket Repository name.


(Optional) Commit ID associated with the source code.

M.U. Ingeniería Informática E.T.S.I. Informática 10


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Plan a Build for AWS CodeBuild

2. Which build commands do you need to run and in what order? By default,
AWS CodeBuild downloads the build input from the provider you specify and
uploads the build output to the bucket you specify. You use the build spec to
instruct how to turn the downloaded build input into the expected build
output.
3. Which runtimes and tools do you need to run the build? For example, are
you building for Java, Ruby, Python, or Node.js? Does the build need Maven or
Ant or a compiler for Java, Ruby, or Python? Does the build need Git, the AWS
CLI, or other tools?

M.U. Ingeniería Informática E.T.S.I. Informática 11


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Plan a Build for AWS CodeBuild

4. Do you need AWS resources that aren't provided automatically by AWS


CodeBuild? If so, which security policies will those resources need? For
example, you might need to modify the AWS CodeBuild service role to allow
AWS CodeBuild to work with those resources.
5. Do you want AWS CodeBuild to work with your VPC? If so, you need the VPC
ID, the subnet IDs, and security group IDs for your VPC configuration.

M.U. Ingeniería Informática E.T.S.I. Informática 12


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create a Build Project in AWS CodeBuild

1. Open the AWS CodeBuild console at


https://console.aws.amazon.com/codesuite/codebuild/home.
2. If an AWS CodeBuild information page is displayed, choose Create project.
Otherwise, on the navigation pane, expand Build, and then choose Build projects.
3. Choose Create build project.
4. In Project configuration:
• On the Create build project page, in Project configuration, for Project name, enter a name for
this build project. Build project names must be unique across each AWS account. You can also
include an optional description of the build project to help other users understand what this
project is used for.
• In Description, enter an optional description for your project.
M.U. Ingeniería Informática E.T.S.I. Informática 13
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create a Build Project in AWS CodeBuild

5. In Source, for Source provider, choose the source code provider type.
• For Amazon S3:
• For Bucket, choose the name of the input bucket that contains the source code.
• For S3 object key or S3 folder, enter the name of the ZIP file or the path to the folder that
contains the source code. Enter a forward slash (/) to download everything in the S3
bucket.

M.U. Ingeniería Informática E.T.S.I. Informática 14


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create a Build Project in AWS CodeBuild

• For AWS CodeCommit :


• From Repository, choose the repository you want to use.
• Choose Git clone depth to create a shallow clone with a history truncated to the specified
number of commits. If you want a full clone, choose Full.

M.U. Ingeniería Informática E.T.S.I. Informática 15


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create a Build Project in AWS CodeBuild

• In Service role, do one of the following:


• If you do not have an AWS CodeBuild service role, choose New service role. In
Role name, accept the default name or enter your own.
• If you have an AWS CodeBuild service role, choose Existing service role. In Role
name, choose the service role.
• If you are not using a VPC for your project, choose No VPC.

M.U. Ingeniería Informática E.T.S.I. Informática 16


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create a Build Project in AWS CodeBuild

6. In Environment:
• For Environment variables, type the name, value, and type of each environment variable for
builds to use. Use Create additional variable to add an environment variable.
• Note
• AWS CodeBuild sets the environment variable for your AWS Region automatically. If you do not add them to
your buildspec.yml, then the following environment variables must be set:
• AWS_ACCOUNT_ID
• IMAGE_REPO_NAME
• IMAGE_TAG
• Others can see environment variables by using the AWS CodeBuild console and the AWS CLI. If
you have no concerns about the visibility of your environment variable, set the Name and Value
fields, and then set Type to Plaintext.
M.U. Ingeniería Informática E.T.S.I. Informática 17
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create a Build Project in AWS CodeBuild

7. In Buildspec:
• For Build specifications, do one of the following:
• If your source code includes a buildspec file, choose Use a buildspec file.
• If your source code does not include a buildspec file, or if you want to run build commands
different from the ones specified for the build phase in the buildspec.yml file in the source
code's root directory, choose Insert build commands.
• For Build commands, enter the commands you want to run in the build phase. For multiple
commands, separate each command by && (for example, mvn test && mvn package). To run
commands in other phases, or if you have a long list of commands for the build phase, add a
buildspec.yml file to the source code root directory, add the commands to the file, and then
choose Use the buildspec.yml in the source code root directory.
M.U. Ingeniería Informática E.T.S.I. Informática 18
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create a Build Project in AWS CodeBuild

8. In Artifacts:
• For Artifacts type, do one of the following:
• If you do not want to create any build output artifacts, choose No artifacts. You might want to do
this if you're only running build tests or you want to push a Docker image to an Amazon ECR
repository.
• To store the build output in an Amazon S3 bucket, choose Amazon S3, and then do the following:
• If you want to use your project name for the build output ZIP file or folder, leave Name blank.
Otherwise, type enter the name. (If you want to output a ZIP file, and you want the ZIP file to
have a file extension, be sure to include it after the ZIP file name.)
• Select Use the name specified in the buildspec file if you want a name specified in the buildspec
file to override any name that is specified in the console.
• For Bucket name, choose the name of the output bucket.
M.U. Ingeniería Informática E.T.S.I. Informática 19
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create a Build Project in AWS CodeBuild

9. Choose Create build project.


10. On the Review page, choose Start build.

M.U. Ingeniería Informática E.T.S.I. Informática 20


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

List of Build Project Names and Project’s Details

1. Open the AWS CodeBuild console at


https://console.aws.amazon.com/codesuite/codebuild/home.
2. In the navigation pane, choose Build projects.
• Note: By default, only the 10 most recent build projects are displayed. To view more build
projects, choose the gear icon, and then choose a different value for Projects per page or
use the back and forward arrows.
3. In the list of build projects, in the Name column, choose the link for the build
project.
4. On the Build project: project-name page, choose Build details.
M.U. Ingeniería Informática E.T.S.I. Informática 21
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create AWS CodeBuild Triggers

• You can create a trigger on a project to schedule a build once every hour, day, or
week.
• You can also create a trigger using a custom rule with an Amazon CloudWatch
cron expression. For example, using a cron expression, you can schedule a build
at a specific time every weekday.
• Choose the link for the build project to which you want to add a trigger, and
then choose the Build triggers tab.
• From the Frequency drop-down list, choose the frequency for your trigger. If you
want to create a frequency using a cron expression, choose Custom.
M.U. Ingeniería Informática E.T.S.I. Informática 22
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Create AWS CodeBuild Triggers

• Optionally, the goal version to build can be chosen.


• Expand Advanced section. In Source version, type a version of your source.
• For Amazon S3, type the version ID that corresponds to the version of the input
artifact you want to build. If Source version is left blank, the latest version is
used.
• For AWS CodeCommit, type a commit ID. If Source version is left blank, the
default branch's HEAD commit ID is used.

M.U. Ingeniería Informática E.T.S.I. Informática 23


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Run a Build in AWS CodeBuild

• Choose Start build for the Build Project you want to run.
• On the Start build page, do one of the following:
• For Amazon S3, for the optional Source version value, type the version ID for the version
of the input artifact you want to build. If Source version is left blank, the latest version is
used.
• For AWS CodeCommit, for the optional Source version value, for Branch, choose the
name of the branch that contains the version of the source code you want to build. For
Source version, accept the displayed HEAD commit ID or enter a different one. If Source
version is blank, the default branch's HEAD commit ID is used.

M.U. Ingeniería Informática E.T.S.I. Informática 24


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Run a Build in AWS CodeBuild

• Under the section Advanced build overrides you can change settings for this build only.
The settings in this section are optional.
• Settings include:
• Source,
• Environment,
• Buildspec,
• Build Artifacts,
• Logs,
• Service Role.
• You can use the section Environment variables override if you want to change the
environment variables for this build only.
M.U. Ingeniería Informática E.T.S.I. Informática 25
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

View Build Details

• You can view a Build Details by either:


• In the navigation pane, choose Build history. In the list of builds, in the Build run column,
choose the link for the build.
• In the navigation pane, choose Build projects. In the list of build projects, in the Name
column, choose the link for the name of the build project. Then, in the list of builds, in the
Build run column, choose the link for the build.

M.U. Ingeniería Informática E.T.S.I. Informática 26


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Phase Transitions

M.U. Ingeniería Informática E.T.S.I. Informática 27


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec File Name and Storage Location

• If you include a build spec as part of the source code, by default, the build spec
file must be named buildspec.yml and placed in the root of your source
directory.
• For S3, the source code in included in a zip file. The build spec file must be in the
root (top level) directory of the zip file.
• For AWS CodeCommit, GitHub, and Bitbucket repositories, by convention, you
must store a build spec file named buildspec.yml in the root (top level) of each
repository or include the build spec declaration as part of the build project
definition. Do not create a ZIP file that contains the repository's source code and
build spec file.
M.U. Ingeniería Informática E.T.S.I. Informática 28
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax


• Build spec files must be expressed in YAML format.
• The build spec has the following syntax:
version: 0.2
run-as: Linux-user-name
env:
variables:
key: "value”
key: "value”
parameter-store:
key: "value”
key: "value"
M.U. Ingeniería Informática E.T.S.I. Informática 29
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax

phases:
install:
run-as: Linux-user-name
commands:
- command
- command
finally: - command - command

M.U. Ingeniería Informática E.T.S.I. Informática 30


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax

pre_build:
run-as: Linux-user-name
commands:
- command
- command
finally: - command - command

M.U. Ingeniería Informática E.T.S.I. Informática 31


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax

build:
run-as: Linux-user-name
commands:
- command
- command
finally: - command - command

M.U. Ingeniería Informática E.T.S.I. Informática 32


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax

post_build:
run-as: Linux-user-name
commands:
- command
- command
finally: - command - command

M.U. Ingeniería Informática E.T.S.I. Informática 33


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax

artifacts:
files:
- location
- location
- name
discard-paths: yes
base-directory: location

M.U. Ingeniería Informática E.T.S.I. Informática 34


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax


secondary-artifacts:
artifactIdentifier:
files:
- location
- location
- name
discard-paths: yes
base-directory: location
artifactIdentifier:
files:
- location
- location
- name
discard-paths: yes
base-directory: location
M.U. Ingeniería Informática E.T.S.I. Informática 35
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax


cache:
paths:
- path
- path

M.U. Ingeniería Informática E.T.S.I. Informática 36


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax Reference

• version: Required mapping. Represents the build spec version. Version 0.2
recommended.
• run-as: Optional sequence. Available to Linux users only. Specifies a Linux user
that runs commands in this buildspec file. If run-as is not specified, then all
commands run as the root.

M.U. Ingeniería Informática E.T.S.I. Informática 37


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax Reference

• env: Optional sequence. Represents information for one or more custom


environment variables.
• variables: Required if env is specified, and you want to define custom
environment variables in plain text. Contains a mapping of key/value scalars.
• parameter-store: Required if env is specified, and you want to retrieve custom
environment variables stored in Amazon EC2 Systems Manager Parameter Store.

M.U. Ingeniería Informática E.T.S.I. Informática 38


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax Reference

• phases: Required sequence. Represents the commands AWS CodeBuild runs


during each phase of the build.
• run-as: Optional sequence. Use in a build phase to specify a Linux user that runs
its commands.

M.U. Ingeniería Informática E.T.S.I. Informática 39


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax Reference


The allowed build phase names are:
• install: Optional sequence. Represents the commands, if any, that AWS CodeBuild runs
during installation.
• pre_build: Optional sequence. Represents the commands, if any, that AWS CodeBuild runs
before the build.
• build: Optional sequence. Represents the commands, if any, that AWS CodeBuild runs during
the build.
• post_build: Optional sequence. Represents the commands, if any, that AWS CodeBuild runs
after the build.
• commands: Required for every specified phase. Contains a sequence of scalars, where each
scalar represents a single command that AWS CodeBuild runs after the build. AWS
CodeBuild runs each command, one at a time, in the order listed, from beginning to end.
M.U. Ingeniería Informática E.T.S.I. Informática 40
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax Reference


The allowed build phase names are:
• finally: Optional block. Commands specified in a finally block are executed after
commands in the commands block. The commands in a finally block are
executed even if a command in the commands block fails. For example, if the
commands block contains three commands and the first fails, AWS CodeBuild
skips the remaining two commands and runs any commands in the finally block.
The phase is successful when all commands in the commands and the finally
blocks run successfully. If any command in a phase fails, the phase fails.
• artifacts: Optional sequence. Represents information about where AWS
CodeBuild can find the build output and how AWS CodeBuild prepares it for
uploading to the Amazon S3 output bucket.
M.U. Ingeniería Informática E.T.S.I. Informática 41
Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Build Spec Syntax Reference


The allowed build phase names are:
• cache: Optional sequence. Represents information about where AWS CodeBuild
can prepare the files for uploading cache to an Amazon S3 cache bucket.

M.U. Ingeniería Informática E.T.S.I. Informática 42


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Exercise

• Create and run a Build Project for a Java program.


• Follow instructions at:
https://docs.aws.amazon.com/codebuild/latest/userguide/getting-started.html.

M.U. Ingeniería Informática E.T.S.I. Informática 43


Desarrollo de Aplicaciones en la Nube
Infrastructure As A Service

Exercise

• Create and run a Build Project for a Python program.


• Follow instructions at:
https://docs.aws.amazon.com/codebuild/latest/userguide/sample-python-hw.ht
ml
.

M.U. Ingeniería Informática E.T.S.I. Informática 44

You might also like