Wednesday, March 27, 2024

How to Deploy Node.js Application to AWS Lambda with Jenkins

In this comprehensive guide, we'll walk through setting up Jenkins to deploy Node.js applications to AWS Lambda seamlessly. By following these steps, you'll be able to automate your deployment process, saving time and ensuring consistency in your workflows.

Step 1: Install Required Plugins

Jenkins thrives on its plugin-based architecture, offering extensive customization and integration capabilities. To deploy applications to AWS from Git, ensure you have the following essential plugins installed:

  • AWS Credentials: Facilitates adding AWS access keys securely to Jenkins credentials.
  • Pipeline: AWS Steps: Provides pipeline steps to interact with the AWS API seamlessly.
  • Blue Ocean: Offers a modern pipeline view for enhanced visualization.

Don't forget to restart Jenkins after installing these plugins to ensure smooth operation.

// Jenkinsfile: Install Required Plugins


pipeline {

    agent any


    stages {

        stage('Install Plugins') {

            steps {

                script {

                    pluginList = ['aws-credentials', 'pipeline-aws', 'blueocean']

                    pluginList.each { plugin ->

                        if (!jenkins.model.Jenkins.instance.pluginManager.plugins.find { it.getShortName() == plugin }) {

                            echo "Installing plugin: $plugin"

                            jenkins.model.Jenkins.instance.pluginManager.install([plugin])

                            jenkins.model.Jenkins.instance.restart()

                        }

                    }

                }

            }

        }

    }

}

Step 2: Jenkins Server Installations

Before diving into deployment, make sure your host machine is equipped with necessary prerequisites. Follow these steps to set up your Jenkins server:

  • SSH into your EC2 instance and update the system before installing Git.
  • Install Git and verify the installation to ensure smooth integration.
  • Install Node.js and TypeScript to compile your Node.js application effectively.


# SSH into your EC2 instance

ssh -i <sshkey_name>.pem ec2-user@<public-ip-of-instance>


# Update the system and install Git

sudo dnf update

sudo yum install git


# Verify Git installation

git --version


# Install Node.js and TypeScript

sudo yum install epel-release

sudo yum install nodejs

sudo npm install -g typescript


# Verify Node.js and TypeScript installation

node -v

npm -v

tsc -v

Step 3: Adding Credentials

Authorization is crucial for Jenkins to access code repositories and deploy to AWS Lambda. Follow these steps to add necessary credentials:

  • Git Credentials: Create a Personal Access Token (PAT) on GitHub and add it to Jenkins credentials to authorize access to private repositories.
  • AWS Credentials: Generate AWS access and secret keys via the AWS console and add them to Jenkins credentials for secure interaction with AWS services.

With credentials configured, you're ready to create your first pipeline.

// Jenkinsfile: Adding Credentials


pipeline {

    agent any


    stages {

        stage('Add Git Credentials') {

            steps {

                withCredentials([usernamePassword(credentialsId: 'git-cred', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {

                    // Add logic to use Git credentials

                }

            }

        }

        stage('Add AWS Credentials') {

            steps {

                withCredentials([usernamePassword(credentialsId: 'aws-cred', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY')]) {

                    // Add logic to use AWS credentials

                }

            }

        }

    }

}

Conclusion

By following this step-by-step guide, you've successfully set up Jenkins to deploy Node.js applications to AWS Lambda. With essential plugins installed, credentials added, and a pipeline configured, you're well on your way to streamlining your deployment process.

Automating deployments with Jenkins pipelines not only saves time but also ensures consistency and reliability in your workflows. Embrace automation to enhance efficiency and productivity in your development journey. Happy coding!

0 comments:

Post a Comment