Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Building Serverless Microservices in Python
Building Serverless Microservices in Python

Building Serverless Microservices in Python: A complete guide to building, testing, and deploying microservices using serverless computing on AWS

eBook
€8.99 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Building Serverless Microservices in Python

Securing your serverless microservices

In this section, we will discuss security in detail.

Lambda security

As we discussed earlier, AWS Lambda is the central component in a serverless stack, or the integration connector with your custom code, triggered by events between AWS managed services. A Lambda function always has an execution IAM role associated with it, and using policies attached to that role is one of the best, and most secure, ways to deny or grant it access to other AWS resources. The beauty is that there is no need to manage or exchange any keys or passwords for a lot of the AWS managed services, such as S3, DynamoDB, and Kinesis Stream. There are some exceptions, such as some of the Amazon Relational Database Service...

Building a serverless microservice data API

In this section, we will look at the architecture and requirements of building a serverless microservice. The rest of the chapter is heavily hands-on in terms of configuration in the AWS Management Console, but also Python code. The Python code follows basic design patterns and is kept simple, so that you can understand and easily adapt it for your own use cases.

Serverless microservice data API requirements

We want to create a microservice that is able to serve a web visits count, for a specific event, of the total users browsing your website.

Setting up Lambda security in the AWS Management Console

We will be signing into the AWS Management Console. The reason we are using the Management Console first is to give you a better understanding of how Lambda functions work, and how they integrate with the other AWS services, such as API Gateway and DynamoDB. In later chapters, we will show you how to deploy Lambda functions using the AWS CLI. If you are a first-timer to Lambda, then I always find it useful to first create a full serverless stack manually in the Management Console to gain a better and deeper understanding than, say, have a magic command spin up the full AWS infrastructure!

We are going to first use the AWS Management Console to create the Lambda IAM role and policies, so that the Lambda function can access DynamoDB, and also write any logs or any statuses to CloudWatch. The Management Console, which we used...

Creating and writing to a NoSQL database called DynamoDB using AWS

We are going to look at creating a DynamoDB table, writing data to the table from hardcoded values, writing data records from a file, and then we are going to show two different ways to query a table:

Creating a DynamoDB in AWS

The following steps show how to create a DynamoDB:

  1. You need to sign in to the AWS Management Console first and then open the AWS DynamoDB console at https://console.aws.amazon.com/dynamodb/.
  2. Choose Create table or, in the DynamoDB navigation pane, choose Tables and choose Create table.
  3. In the Create DynamoDB Table window, perform the following steps:
    1. Under Table name, type user-visits
    2. In Primary key for Partition key, type EventId and...

Creating and writing to a NoSQL database called DynamoDB using Python

Now that we understand how to create a table, add data, and query DynamoDB using the AWS Console, we will look at how we can do this using only Python code.

We recommend you use a Python Integrated development environment (IDE) such as Eclipse PyDev (http://www.pydev.org/download.html) or PyCharm (https://www.jetbrains.com/pycharm/). You do not need to use an IDE, but I would recommend that you do. If you really want to, you can use VI, for example, on Linux to actually edit your code. But using an IDE allows you, for example, to run debugging or set up unit testing locally and step through it, which makes it easier and more productive for development.

First create the table using Boto3 https://boto3.readthedocs.io/ in Python. Run the code in the following section in PyCharm or your favorite text editor...

Creating a Lambda to query DynamoDB

Now that we have the security and user-visits table set up with some data, and know how to write code to query that DynamoDB table, we will write the Lambda Python code.

Creating the Lambda function

Now we have the IAM role with two IAM policies attached, create the Lambda function itself. Here, we are creating a function from scratch, as we want to walk through the full details to deepen your understanding of what is involved in creating a serverless stack. The following diagram shows data API architecture involving CloudWatch, DynamoDB, IAM, and Lambda:

Perform the following steps:

  1. Sign in to the AWS Management Console and open the AWS Lambda console at https://console.aws.amazon.com/lambda/...

Setting up the API Gateway and integrating it with a Lambda proxy

Now that we know the Lambda function works with some API Gateway test data, and returns a header and body with a statusCode of 200, we just need to add the API Gateway that will invoke the Lambda function, as shown in the following diagram:

Perform the following steps:

  1. Sign in to the AWS Management Console and open the API Gateway console at https://console.aws.amazon.com/apigateway/.
  2. Choose Get Started or, in the Amazon API Gateway navigation pane, choose APIs and choose Create API.
  3. On the Create page, perform the following steps:
    1. In Choose Protocol, select REST
    2. In Create new API, select New API
    3. Under Settings, type metrics for API name
    4. Choose Regional for Endpoint Type
    5. Choose Create API
  4. Choose Create Resource from the Actions drop-down menu.
  5. In the New Child Resource window, perform the following steps...

Connecting API Gateway, Lambda, and DynamoDB

Now that we know the API Gateway integration with the Lambda function works, we will deploy it and get the URL. The architecture is shown in the following diagram:

The workings of this architecture is as follows:

  1. Sign in to the AWS Management Console and open the API Gateway console at https://console.aws.amazon.com/apigateway/.
  2. In the Amazon API Gateway navigation pane, choose APIs and metrics.
  3. Select Resources under metrics and /Vists/{resourceId}, and choose Deploy API from the Actions drop-down menu.
  4. In the Deploy API pop-up window, perform the following steps:
    1. In Deployment stage, choose [New Stage]
    2. In Stage name, type prod
    3. In Stage description, type prod
    4. Select Deploy
  5. The Stages under metrics should be automatically selected on the left-hand menu.
  6. Select GET under prod/visits/{resourceId}/GET to get the invoke URL. The invoke...

Cleaning-up

You will need to delete the resources manually. I recommend you use the AWS Console to do so. Perform the following steps:

  1. Deleting API Gateway:
    1. Log on to the Console at https://console.aws.amazon.com/apigateway/
    2. Choose Resource under metrics on the left-hand APIs menu
    3. Choose Delete API from the Actions drop-down menu
    4. Type metrics in the Enter the name of the API before confirming this action textbox
    5. Choose Delete API
  2. Deleting the DynamoDB table:
    1. Log on to the Console at https://console.aws.amazon.com/dynamodb/
    2. Choose Tables on the left-hand DynamoDB menu
    3. Choose user-visits
    4. Choose Delete table
    5. Choose Delete
  1. Deleting the Lambda function:
    1. Log on to the Console at https://console.aws.amazon.com/lambda/
    2. Choose Functions on the left-hand AWS Lambda menu
    3. Choose lambda-dynamo-data-api
    4. Choose Delete function under the Actions menu
    5. Choose Delete
  2. Deleting...

Summary

In this chapter, we have discussed security and why it is important. Applying the OWASP security by design principles is a good first step to ensure that your serverless stack is secure. We then discussed IAM roles and gave an overview of policies, explaining how they are the key documents to ensure restricted access to AWS resources. We then looked at an overview of some of the security concepts and principles regarding securing your serverless microservices, specifically regarding Lambda, API Gateway, and DynamoDB.

We then built a scalable serverless microservice with a RESTful data API. We started by creating a DynamoDB table, then added data to it, and queried it, first using the AWS console manually, then using the Python Boto3 SDK. We then built a simple Lambda to parse the request URL parameters, query DynamoDB, and return the records as part of a response body....

Setting up Lambda security in the AWS Management Console


We will be signing into the AWS Management Console. The reason we are using the Management Console first is to give you a better understanding of how Lambda functions work, and how they integrate with the other AWS services, such as API Gateway and DynamoDB. In later chapters, we will show you how to deploy Lambda functions using the AWS CLI. If you are a first-timer to Lambda, then I always find it useful to first create a full serverless stack manually in the Management Console to gain a better and deeper understanding than, say, have a magic command spin up the full AWS infrastructure!

We are going to first use the AWS Management Console to create the Lambda IAM role and policies, so that the Lambda function can access DynamoDB, and also write any logs or any statuses to CloudWatch. The Management Console, which we used earlier, in Chapter 1, Serverless Microservices Architectures and Patterns, allows you to centrally control all...

Creating and writing to a NoSQL database called DynamoDB using AWS


We are going to look at creating a DynamoDB table, writing data to the table from hardcoded values, writing data records from a file, and then we are going to show two different ways to query a table:

Creating a DynamoDB in AWS

The following steps show how to create a DynamoDB:

  1. You need to sign in to the AWS Management Console first and then open the AWS DynamoDB console at https://console.aws.amazon.com/dynamodb/.
  2. Choose Create table or, in the DynamoDB navigation pane, choose Tables and choose Create table.
  3. In the Create DynamoDB Table window, perform the following steps:
    1. Under Table name, type user-visits
    2. In Primary key for Partition key, type EventId and choose String
    3. Check the Add sort key box
    4. In Sort Key, type EventDay and choose Number

The partition key and hash key can be used interchangeably, like sort key and range keys. A primary key can be the partition key alone, or a composite key with both a partition key and a sort...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Create a secure, cost-effective, and scalable serverless data API
  • Use identity management and authentication for a user-specific and secure web application
  • Go beyond traditional web hosting to explore the full range of cloud hosting options

Description

Over the last few years, there has been a massive shift from monolithic architecture to microservices, thanks to their small and independent deployments that allow increased flexibility and agile delivery. Traditionally, virtual machines and containers were the principal mediums for deploying microservices, but they involved a lot of operational effort, configuration, and maintenance. More recently, serverless computing has gained popularity due to its built-in autoscaling abilities, reduced operational costs, and increased productivity. Building Serverless Microservices in Python begins by introducing you to serverless microservice structures. You will then learn how to create your first serverless data API and test your microservice. Moving on, you'll delve into data management and work with serverless patterns. Finally, the book introduces you to the importance of securing microservices. By the end of the book, you will have gained the skills you need to combine microservices with serverless computing, making their deployment much easier thanks to the cloud provider managing the servers and capacity planning.

Who is this book for?

If you are a developer with basic knowledge of Python and want to learn how to build, test, deploy, and secure microservices, then this book is for you. No prior knowledge of building microservices is required.

What you will learn

  • Discover what microservices offer above and beyond other architectures
  • Create a serverless application with AWS
  • Gain secure access to data and resources
  • Run tests on your configuration and code
  • Create a highly available serverless microservice data API
  • Build, deploy, and run your serverless configuration and code

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 29, 2019
Length: 168 pages
Edition : 1st
Language : English
ISBN-13 : 9781789533422
Languages :
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Mar 29, 2019
Length: 168 pages
Edition : 1st
Language : English
ISBN-13 : 9781789533422
Languages :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 99.97
Python Microservices Development
€41.99
Hands-On Docker for Microservices with Python
€32.99
Building Serverless Microservices in Python
€24.99
Total 99.97 Stars icon
Banner background image

Table of Contents

7 Chapters
Serverless Microservices Architectures and Patterns Chevron down icon Chevron up icon
Creating Your First Serverless Data API Chevron down icon Chevron up icon
Deploying Your Serverless Stack Chevron down icon Chevron up icon
Testing Your Serverless Microservice Chevron down icon Chevron up icon
Securing Your Microservice Chevron down icon Chevron up icon
Summary and Future Work Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(5 Ratings)
5 star 20%
4 star 20%
3 star 20%
2 star 20%
1 star 20%
User Sep 16, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Found this is a really good book to get me up and running with Serverless in AWS using Python, wish it were bit longer. Really like how the author introduces microservice architecture theory, with practical examples I can use and the in-depth and hands-on section on many forms of testing.
Amazon Verified review Amazon
Amazon Customer May 06, 2019
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
best for aws users trying to learn python scripting
Amazon Verified review Amazon
Andres G. T. Jul 25, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
It has a good intro to microservices concepts and good practice with lambda. However, if you plan to follow the exercises you may find it difficult. For example, sometimes the code it's repetitive and the author doesn't explain why, or he doesn't tell you that, after writing some python scripts, you have to run them locally first before running a test for a lambda function in AWS. Also, the GitHub site for the book is hard to find the code for the corresponding section in the book (at the least in the Kindle edition).
Amazon Verified review Amazon
Rob Shearer Apr 08, 2021
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
This can't really be called a "book". It's a tutorial, where you follow the directions given to create an AWS serverless service. There is a brief description of what each command does, but very little discussion of why or what other approaches are possible. Many of the steps are literally "cut and paste this code into the box".The real issue is that even as a tutorial it's atrocious. The quality of the code itself wouldn't pass muster in any serious programming team. There seems to be no rhyme or reason to what is abstracted or parameterized, leading to code that is overcomplicated and verbose while providing no real value for reuse. But it's also terribly written for a tutorial. When code from one step is expanded with a little bit of additional functionality in the next step, that isn't added as a few extra lines of code; instead the entire code is arbitrarily rewritten with slightly different function names for no obvious reason. There was clearly no intention of readers ever actually "working through" the tutorial by coding it up themselves (and experimenting with variations), only cutting and pasting.I wish I had the knowledge to comment on whether the tutorial genuinely offers modern best practice for developing serverless applications. I don't. But the author's obvious lack of any production software engineering expertise or experience does not inspire confidence.That this "book" was pushed out the door in this state must be taken as a reflection of its publisher. Based on this book, I will make sure not to waste money on any other Packt releases in the future.
Amazon Verified review Amazon
utente Sep 09, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Terribile. È scritto coi piedi. Sembra il risultato di una persona a cui è stato detto che dovrebbe scrivere un libro, e ha buttato giù quattro appunti. Termini usati senza definirli, o con la definizione dopo il primo utilizzo. Frasi senza capo ne coda. Mi sono fermato quando ho visto una immagine in bianco e nero con la spiegazione: "I rettangoli in verde indicano i servizi...". QUesto genere di libri distruggono la fiducia in un autore e in una casa editrice. Non li conoscevo prima, li eviterò in seguito. Mi spiace perchè l'argomento mi interessava davvero :-(
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.