- AWS account.
- Install Git on your local machine.
- Go to the AWS Management Console and search for "CodeCommit".
- Create a new repository in CodeCommit
- navigate to IAM user and secure the credentials (Username and password) to access AWS code commit.
-
Start by initializing a Git repository on your local machine. Open a terminal, navigate to your project directory, and run the command
git init
to set up the repository. -
Add your Node.js app files to the Git repository. Use the command
git add .
to include all files in the repository.
After populating the node.js files with the required contents, npm and node.js was installed. npm init
was run to initiailise the project directory and create the package.json file. npm install express
was run afterwards.
-
Commit your changes to the repository. Execute the command
git commit -m "Initial commit"
to create a commit with an appropriate commit message. -
Next, establish a connection to the remote CodeCommit repository. Obtain the "HTTPS clone URL" from the CodeCommit repository page. Use the command
git remote add origin <clone_url>
to link your local repository to the remote repository, replacing <clone_url> with the actual URL. -
Finally, push your code to CodeCommit. Execute
git push origin master
to push the committed changes to the master branch. If prompted, provide your AWS CodeCommit credentials to complete the push.
By following these steps, I successfully configured Git, added my Node.js app files to the repository, commited my changes, set up the remote repository connection, and pushed my code to CodeCommit.
-
Open the AWS Management Console and search for "CodeBuild".
-
Click on "Create build project" to start the project creation process.
-
Configure the source settings:
Select the source provider, AWS CodeCommit in this case
Choose the repository ("Trio") and branch (master) to use.
Specify the buildspec file location (buildspec.yml)
-
Configure the environment settings:
Select the operating system, runtime, and compute type for the build environment.
Choose whether to use a managed image or a custom Docker image. Managed image was used.
Specify any additional environment variables needed for your build process.
-
Configure the optional notifications:
Specify whether to receive build notifications via Amazon SNS.
Choose the SNS topic and events for which you want to be notified.
-
Review the project settings to ensure they match your requirements.
-
Click "Create build project" to create the AWS CodeBuild project.
-
Click on the "Create pipeline" button and proceed with the following steps:
Provide a name for your pipeline.
Choose "AWS CodeCommit" as the source provider.
Select the desired repository and branch to use.
Opt for "AWS CodeBuild" as the build provider. Select the earlier created project (node-js-app)
-
The deployment stage was skipped. We shall get to it.
-
Review the settings of the pipeline and click "Create pipeline" to create it.
-
Upon a new commit to the AWS code commit repository created, the pipeline is triggered.
-
The pipeline stages was successful
- The image was successfully pushed to my private repo in ECR.
The project was executed in two stages;
- Running the node.js application
- Building a docker image for the nodejs application from Dockerfile and pushing the image to AMAZON ECR.
Here's a view of the build stages as specified in the buildspec.yml file
While running the node.js application, the pipeline got stuck after running the "npm run start" stage.
I discovered that the npm scripts specified node index.js
as the command to run in the start stage. This hijacks the session as the command is run in the foreground thus not returning control back to the pipeline. As a result, the subsequent stages in your pipeline are not executed.
- I utilised "pm2", a process manager to start the node index.js command in the background.
pm2 start index.js
instead ofnode index.js
- The build stage of the buildspec.yml file was also updated to install pm2
- npm install pm2 --save-dev
An error was encountered when I attempted to push the image to Amazon ECR as I created a public repository earlier. I had to resort to using a private repository in ECR and modified my code snippet login command. The error is as shown below. Failed to get authorization tokens even after the necessary policies (AWSEC2ContainerRegistryFullAccess) were attached to the code build service role.
END