SECOND EDITION Michael Wittig Andreas Wittig Foreword by Ben Whaley M A N N I N G Compute & Networking Abbr. Name Description Where EC2 Amazon Elastic Compute Cloud Virtual machines with Linux and 3 Windows AWS Lambda Run code without the need for virtual 7 machines EIP Elastic IP Address Fixed public IP address for EC2 3.6 instances ENI Amazon EC2 Elastic Network Interface Virtual network interface for EC2 3.7 instances VPC Amazon Virtual Private Cloud Private network inside the cloud 6.5 Amazon EC2 Security Group Network firewall 6.4 Deployment & Management Abbr. Name Description Where AWS Elastic Beanstalk Deployment tool for simple applications 5.4 AWS OpsWorks Deployment tool for multilayer applica- 5.5 tions AWS CloudFormation Infrastructure automation and deploy- 5.3 ment tool IAM AWS Identity and Access Management Secure access to your cloud resources 6.3 (authentication and authorization) CLI AWS command-line interface AWS in your terminal 4.2 SDK AWS software development kits AWS in your applications 4.3 Praise for the First Edition Fantastic introduction to cloud basics with excellent real-world examples. —Rambabu Posa, GL Assessment A very thorough and practical guide to everything AWS ... highly recommended. —Scott M. King, Amazon Cuts through the vast expanse of official documentation and gives you what you need to make AWS work now! —Carm Vecchio, Computer Science Corporation (CSC) The right book to program AWS from scratch. —Javier Muñoz Mellid, Senior Computer Engineer, Igalia Amazon Web Services in Action, Second Edition MICHAEL WITTIG ANDREAS WITTIG F BEN WHALEY OREWORD BY MANNING Shelter Island For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2019 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. The following are trademarks of Amazon.com, Inc. or its affiliates in the United States and/or other countries: Amazon Web Services, AWS, Amazon EC2, EC2, Amazon Elastic Compute Cloud, Amazon Virtual Private Cloud, Amazon VPC, Amazon S3, Amazon Simple Storage Service, Amazon CloudFront, CloudFront, Amazon SQS, SQS, Amazon Simple Queue Service, Amazon Simple Email Service, Amazon Elastic Beanstalk, Amazon Simple Notification Service, Amazon Route 53, Amazon RDS, Amazon Relational Database, Amazon CloudWatch, AWS Premium Support, Elasticache, Amazon Glacier, AWS Marketplace, AWS CloudFormation, Amazon CloudSearch, Amazon DynamoDB, DynamoDB, Amazon Redshift, and Amazon Kinesis. The icons in this book are reproduced with permission from Amazon.com or under a Creative Commons license as follows: ■ AWS Simple Icons by Amazon.com (https://aws.amazon.com/architecture/icons/) ■ File icons by Freepik (http://www.flaticon.com/authors/freepik) License: CC BY 3.0 ■ Basic application icons by Freepik (http://www.flaticon.com/authors/freepik) License: CC BY 3.0 All views expressed in this book are of the authors and not of AWS or Amazon. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Frances Lefkowitz 20 Baldwin Road Technical development editor John Hyaduck PO Box 761 Review editor: Aleksandar Dragosavljevic´ Shelter Island, NY 11964 Project editor: Deirdre Hiam Copy editor: Benjamin Berg Proofreader: Elizabeth Martin Technical proofreader: David Fombella Pombal Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617295119 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – DP – 23 22 21 20 19 18 brief contents P 1 G ............................................................1 ART ETTING STARTED 1 ■ What is Amazon Web Services? 3 2 ■ A simple example: WordPress in five minutes 36 P 2 B ART UILDING VIRTUAL INFRASTRUCTURE CONSISTING OF COMPUTERS ..........................................................57 AND NETWORKING 3 ■ Using virtual machines: EC2 59 4 ■ Programming your infrastructure: The command-line, SDKs, and CloudFormation 102 5 ■ Automating deployment: CloudFormation, Elastic Beanstalk, and OpsWorks 135 6 ■ Securing your system: IAM, security groups, and VPC 165 7 ■ Automating operational tasks with Lambda 199 P 3 S .........................................233 ART TORING DATA IN THE CLOUD 8 ■ Storing your objects: S3 and Glacier 235 9 ■ Storing data on hard drives: EBS and instance store 258 v vi BRIEF CONTENTS 10 ■ Sharing data volumes between machines: EFS 274 11 ■ Using a relational database service: RDS 294 12 ■ Caching data in memory: Amazon ElastiCache 321 13 ■ Programming for the NoSQL database service: DynamoDB 349 P 4 A AWS................................................381 ART RCHITECTING ON 14 ■ Achieving high availability: availability zones, auto-scaling, and CloudWatch 383 15 ■ Decoupling your infrastructure: Elastic Load Balancing and Simple Queue Service 413 16 ■ Designing for fault tolerance 431 17 ■ Scaling up and down: auto-scaling and CloudWatch 463 contents foreword xvii preface xix acknowledgments xxi about this book xxiii about the author xxvii about the cover illustration xxviii P 1 G ..................................................1 ART ETTING STARTED 1 What is Amazon Web Services? 3 1.1 What is cloud computing? 4 1.2 What can you do with AWS? 5 Hosting a web shop 5 ■ Running a Java EE application in your private network 7 ■ Implementing a highly available system 8 Profiting from low costs for batch processing infrastructure 9 1.3 How you can benefit from using AWS 10 Innovative and fast-growing platform 10 ■ Services solve common problems 10 ■ Enabling automation 10 ■ Flexible capacity (scalability) 11 ■ Built for failure (reliability) 11 ■ Reducing time to market 11 ■ Benefiting from economies of scale 12 Global infrastructure 12 ■ Professional partner 12 vii viii CONTENTS 1.4 How much does it cost? 12 Free Tier 13 ■ Billing example 13 ■ Pay-per-use opportunities 15 1.5 Comparing alternatives 15 1.6 Exploring AWS services 16 1.7 Interacting with AWS 19 Management Console 19 ■ Command-line interface 20 SDKs 21 ■ Blueprints 22 1.8 Creating an AWS account 22 Signing up 23 ■ Signing In 28 ■ Creating a key pair 29 1.9 Create a billing alarm to keep track of your AWS bill 33 2 A simple example: WordPress in five minutes 36 2.1 Creating your infrastructure 37 2.2 Exploring your infrastructure 44 Resource groups 44 ■ Virtual machines 45 ■ Load balancer 47 ■ MySQL database 49 ■ Network filesystem 50 2.3 How much does it cost? 52 2.4 Deleting your infrastructure 54 P 2 B - ART UILDING VIRTUAL INFRASTRUCTURE CONSISTING OF COM ......................................57 PUTERS AND NETWORKING 3 Using virtual machines: EC2 59 3.1 Exploring a virtual machine 60 Launching a virtual machine 60 ■ Connecting to your virtual machine 72 ■ Installing and running software manually 75 3.2 Monitoring and debugging a virtual machine 76 Showing logs from a virtual machine 76 ■ Monitoring the load of a virtual machine 77 3.3 Shutting down a virtual machine 78 3.4 Changing the size of a virtual machine 79 3.5 Starting a virtual machine in another data center 82 3.6 Allocating a public IP address 86 3.7 Adding an additional network interface to a virtual machine 88 3.8 Optimizing costs for virtual machines 92 Reserve virtual machines 93 ■ Bidding on unused virtual machines 95