Amazon - download.e- · PDF fileAmazon Cognito User Pools 187 Introducing Amazon Cognito User...

Click here to load reader

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Amazon - download.e- · PDF fileAmazon Cognito User Pools 187 Introducing Amazon Cognito User...

  • AmazonWeb Services for

    Mobile DevelopersBuilding Apps with AWS

    Abhishek Mishra

  • Senior Acquisitions Editor: Kenyon Brown Development Editor: Tom Dinse Technical Editors: Chaim Krause and John Mueller Production Editor: Christine OConnor Copy Editor: Karen Davis Editorial Manager: Mary Beth WakefieldProduction Manager: Kathleen WisorAssociate Publisher: Jim MinatelBook Designers: Judy Fung and Bill Gibson Proofreader: Nancy CarrascoIndexer: Ted Laux Project Coordinator, Cover: Brent SavageCover Designer: WileyCover Image: Jeremy Woodhouse / Getty Images

    Copyright 2018 by John Wiley & Sons, Inc., Indianapolis, Indiana

    Published simultaneously in Canada

    ISBN: 978-1-119-37785-6 ISBN: 978-1-119-37784-9 (ebk.)ISBN: 978-1-119-37786-3 (ebk.)

    Manufactured in the United States of America

    No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit-ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at

    Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or war-ranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.

    For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at (877) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002.

    Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at For more information about Wiley prod-ucts, visit

    Library of Congress Control Number: 2017956046

    TRADEMARKS: Wiley, the Wiley logo, and the Sybex logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Amazon Web Services and AWS are trademarks or registered trademarks of Amazon Technologies, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

    10 9 8 7 6 5 4 3 2 1

  • To my wife, Sonam, for her love and support through all the years weve

    been together.

    To my daughter, Elana, for bringing joy and happiness into our lives.

  • AcknowledgmentsThis book would not have been possible without the support of the team at Wiley, including Jim Minatel, Kenyon Brown, Tom Dinse, and Karen Davis.

    I would also like to thank Chaim Krause for taking the time to read the entire manuscript and for his keen eye for detail.

    It has been my privilege to work with you. Thank you.

    About the AuthorAbhishek Mishra has been active in the IT industry for more than 19 years. He has extensive experience with a wide range of programming languages, enterprise systems, and platforms.

    He holds a masters degree in computer science from the University of London and currently provides consultancy services to Barclays Bank PLC in London as a solutions architect.

    Abhishek is the author of iOS Code Testing, iPhone and iPad App: 24-Hour Trainer, and Swift iOS: 24-Hour Trainer. He is the technical reviewer of Professional iOS Programming.

    About the Technical EditorChaim Krause is a simulation specialist for the U.S. Army in Leavenworth, Kansas. Although he holds a bachelors in political science from the University of Chicago, Chaim is an autodidact when it comes to computers, programming, and electronics. He wrote his first computer game in BASIC on a Tandy Model I Level I and stored the program on a cassette tape. Amateur radio introduced him to electronics, while the Arduino and the Raspberry Pi provided a medium to combine computing, programming, and electronics into one hobby.

    In his spare time, Chaim likes to play PC games and occasionally develops his own. He has recently taken up the sport of golf to spend more time with his significant other, Ivana.

  • Contents at a GlanceIntroduction xix

    Part I Introduction to Amazon Web Services 1

    Chapter 1 Introduction to Cloud Computing and Amazon Web Services 3

    Chapter 2 Regions, Availability Zones, and Edge Locations 19

    Chapter 3 AWS Identity and Access Management 33

    Chapter 4 Amazon EC2 61

    Chapter 5 Amazon S3 101

    Chapter 6 Amazon DynamoDB 125

    Chapter 7 AWS Lambda 145

    Bonus Chapter 1 Amazon Simple Notification Service 1

    Bonus Chapter 2 Amazon Mobile Analytics 1

    Bonus Chapter 3 AWS Device Farm 1

    Bonus Chapter 4 Installing WordPress on Amazon EC2 1

    Part II AWS for iOS Developers 167

    Chapter 8 Integrating the AWS SDK for iOS 169

    Chapter 9 Implementing UserSignup and Login Using Amazon Cognito User Pools 187

    Chapter 10 Implementing Login Using Facebook 233

    Chapter 11 Implementing Login Using Google 269

    Chapter 12 Accessing Amazon DynamoDB 293

    Chapter 13 Adding AWSChat Support with Amazon DynamoDB and Amazon S3 363

    Chapter 14 Using AWS Lambda to Generate Thumbnails 405

    Bonus Chapter 5 Adding Support for Apple Push Notifications 1

    Bonus Chapter 6 Integrating Amazon Mobile Analytics with the iOS App 1

    Part III AWS for Android Developers 435

    Chapter 15 Integrating the AWS SDK for Android with Android Studio 437

  • Chapter 16 Implementing User Signup and Login Using Amazon Cognito User Pools 445

    Chapter 17 Implementing Login Using Facebook 497

    Chapter 18 Implementing Login Using Google 535

    Chapter 19 Accessing Amazon DynamoDB 577

    Chapter 20 Adding AWSChat Support with Amazon DynamoDB and Amazon S3 657

    Chapter 21 Using AWS Lambda to Generate Thumbnails 711

    Bonus Chapter 7 Adding Support for GCM Notifications 1

    Bonus Chapter 8 Integrating Amazon Mobile Analytics with the Android App 1

    Index 747

    viii Contents at a Glance

  • ContentsIntroduction xix

    Part I Introduction to Amazon Web Services 1

    Chapter 1 Introduction to Cloud Computing and Amazon Web Services 3

    What Is Cloud Computing? 4Cloud Service Models 5Cloud Deployment Models 7The AWS Ecosystem 7Sign Up for an AWS Free Tier Account 10

    Step 1: Contact Information 10Step 2: Payment Information 12Step 3: Identity Verification 14Step 4: Support Plan Selection 16Step 5: Confirmation 17

    Summary 17

    Chapter 2 Regions, Availability Zones, and Edge Locations 19

    Regions and Availability Zones 20Edge Locations 22Accessing AWS 24

    The AWS Management Console 25Summary 31

    Chapter 3 AWS Identity and Access Management 33

    Key Concepts 34Root Account 34User 35Group 37Policy 38Role 38

    Common Tasks 39Creating a User 42Assigning Permissions to an Existing User 47Creating a Group 49Adding Users from a Group 50Creating a Role 51Securing the Root Account with MFA 54Setting Up an IAM Password Rotation Policy 57

    Summary 59

  • x Contents

    Chapter 4 Amazon EC2 61

    Key Concepts 62Instances and Instance Types 62Amazon Machine Images 65Security Groups 66Pricing Models 67Data Storage 68Instance Life Cycle 68

    Common Tasks 70Creating an Instance 70Managing Existing Instances 81

    Accessing Amazon EC2 Instances Using the AWS CLI 88Linux, Mac OSX, Unix Users 88Windows Users 92

    Summary 100

    Chapter 5 Amazon S3 101

    Key Concepts 102Buckets 102Object Key 102Object Value 103Version ID 103Storage Class 103Costs 104Subresources 104Object Metadata 105

    Common Tasks 106Creating a Bucket 106Uploading an Object 110Accessing an Object 114Changing the Storage Class of an Object 118Deleting an Object 118Amazon S3 Bucket Versioning 119Accessing Amazon S3 Using the AWS CLI 123

    Summary 124

    Chapter 6 Amazon DynamoDB 125

    Key Concepts 126Tables 126Items 126Attributes 127Primary Keys 127Secondar