When it comes to deploying lambda function into AWS, I have been using mostly Java for the implementation language and runtime. However, using Python has some advantages, especially if the code does not require parallel threads. E.g., the deployment package is usually a lot smaller and waking the lambda function, after it hasn’t been used for a while seems faster.
In this video, I compare two AWS services for data preparation: AWS Glue Data Brew and Amazon SageMaker Data Wrangler. I discuss their unique capabilities, a. Brew search aws The package manager will spit out a list of matches. Browse for the right one and then install it using the exact name in the results.
My preferred build automation system for Java-based projects is gradle and I found the com.github.kaklakariada.aws-sam-deploy plugin to be the most convenient way, to quickly build and deploy a stack. Basically a one-click thing, once the credentials are stored in ~/.aws/credentials and the project has a proper template.yaml file.
Even longer than gradle, IntelliJ Idea is me preferred IDE for all things Java, and while JetBrains offers a plugin to use IntelliJ for python development, I find their dedicated IDE for Python, aka PyCharm easier to use.
What follows are the steps to use PyCharm and Amazon’s Command-line -tools or alternatively JetBrains’ AWS ToolKit to create an environment for convenient deployment into AWS.
- Creating an AWS Account
- Installing AWS CLI and AWS SAM CLI
- Deploying into AWS
- Deploying with command tools – or –
- Deploying with JetBrains’ AWS Toolkit
AWS Accounts (aka Root User Accounts) can be created here:
https://portal.aws.amazon.com/gp/aws/developer/registration/index.html students should start here instead: https://aws.amazon.com/education/awseducate/
A credit card number and working phone is needed during the setup process. Select “Basic” for the support plan.
1.1 Creating an IAM Account
I then created an IAM Account (aka AWS Identity and Access Management (IAM) user) here:
https://console.aws.amazon.com/iam
1.2 Group
Now I select ‘Group’ on the left side and then click the “Create new Group” button.
Enter a name (e.g. MyEduGroup) and in the next step add these two policies. As I found out just recently, policies for a specific Lambda function can be referenced in its template.yml file, which I will come back to at the end of this post. It’s still good to know right now that policies can be attached to a function instead of attaching it to a group or a user.
- IAMFullAccess
- PowerUserAccess
Review and click the “Create Group” button.
1.3 User
Brew Awscli
Select ‘User’ on the left side and then click the “Add user” button, to create a new IAM user.
Create a user name (e.g. MyEduUser) and select “Programmatic access” only.
Next, add the user to the group, mentioned above, (e.g. MyEduGroup)
Review and click the “Create user” button and don’t forget to download the csv file, containing the credentials. Add those credentials into a (hidden) file: ~/.aws/credentials file, which should then look like so:
Btw, the string inside the square brackets is called the profile name Self cutting tap for 22mm pipe.
1.4 Bucket
While in the AWS web interface, click in Services and navigate/find S3. There you will have the chance to create a bucket. Do that. I used ‘edu-bucket’ and clicked through, going with all the defaults.
Jetbrains’ AWS Toolkit is basically just a UI accessing the AWS Command-line Tools, which therefore must be installed before we can use the Toolkit.
2.1 AWS CLI
How to install the AWS CLI version 2 is explained here. However, with Homebrew installed on your Mac, it can be installed like so:
and confirming the installation like so:
2.2 AWS SAM CLI
How to install the AWS SAM CLI is explained here. Again, with Homebrew installed on your Mac, it can be installed like so:
and confirming the installation like so:
3.1 Deploying with Command-line Tools
Brew Aws Cli V2
Still using the command line, I create a new folder and excuse ‘sam init’:
which starts collecting some information, before proceeding:
I chose (1) for AWS Quick Start Templates
python3.7 for the runtime
entered hello for a project name
and (1) Hello World Example for the quick start application template
All that led to the following output:
The sam init command initializes a serverless application with a SAM template and some boilerplate code. Here is what the folder/file structure looks like.
With the AWS credentials stored in the ~/.aws/credentials file and the previously created s-3 bucket, the following deploy script can be used to create an AWS CloudFormation stack and deploy the lambda function.
Creating and running the deploy script in the project folder will result in something like this:
After finding the endpoint the the output above, a tool like httpie can be used to quickly run a request:
Of course the just created stack shows up in the AWS Web GUI as well:
Let’s delete the Stack and do the same with PyCharm and the AWS ToolKit installed.
3.2 Deploying with JetBrains’ AWS Toolkit
After launching PyCharm start with installing the AWS Toolkit plugin. Re-launch PyCharm and create a new project, selecting “AWS Serverless Application” application type. Just like before, I select python 3.7 and the “AWS SAM Hello World” SAM Template, and let PyCharm create the project structure.
At this point a dialog pops up asking me if I want to create a project from existing source?
Here I click [NO]
A now already familiar looking folder/file structure will appear. Open the ‘AWS Explorer’ Tab at the lower left side of the IDE. Click on Configure AWS connection and select your profile and region, after which at least the CloudWatch logs from before (we forgot to clean those up) will show up.
Here is the ToolKit’s User Guide. 180 telugu movie songs.
Clicking the AWSExplorer tab followed by the options icon, allows to configure the AWS connection. I.e. the AWS Profile (e.g. ‘edu’) and Region (e.g. ‘us-west-2’) need to be set.
Brew Aws-okta
To quickly create the stack and deploy the lambda function, simply right-click the ‘template.yaml’ file and select ‘Deploy Serverless Application’. Vhd to tib. In the following dialog I provided a stack name, this time Edu-Stack1 and in the ‘CloudFormation’ tab at the very bottom of the IDE, the process of the deployment can be monitored.
After refreshing the AWS Explorer tab, the Edu-Stack1 and the lambda function shows up as well. A right-click on the function followed by “Run” will allow you to invoke the function.
Let’s open app.py, the python script that contains the actual lambda function, and make a code change. E.g. change the message t0 “Hello Serverless World”. Now, right-click the ‘template.yaml’ file and select ‘Deploy Serverless Application’ again. This time Update Stack is pre-selected and clicking Deploy succeeds a little quicker than before.
A right-click on the function followed by “Run” will show the returned response, of the updated lambda function.