]]> create an AWS Lambda function to make the application a distributed systemhttp://www.tkk7.com/Noah/articles/433174.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 03:26:00 GMThttp://www.tkk7.com/Noah/articles/433174.htmlhttp://www.tkk7.com/Noah/comments/433174.htmlhttp://www.tkk7.com/Noah/articles/433174.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433174.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433174.html闃呰鍏ㄦ枃
]]>use AWS X-Ray to collect diagnostics informationhttp://www.tkk7.com/Noah/articles/433173.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 03:23:00 GMThttp://www.tkk7.com/Noah/articles/433173.htmlhttp://www.tkk7.com/Noah/comments/433173.htmlhttp://www.tkk7.com/Noah/articles/433173.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433173.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433173.htmlIn this exercise, you will analyze and collect diagnostics information related to your application with AWS X-Ray. With AWS X-Ray, you can understand how your application and its underlying services are performing to identify and troubleshoot the root cause of performance issues. AWS X-Ray traces user requests as they travel through your entire application. It then aggregates the data generated by the individual services and resources that make up your application, providing you with a trace map of how your application is performing. Note: Make sure to sign in to your AWS account with the AWS IAM useredXProjectUsercredentials.
To begin, follow the steps below.
1. Start the RDS database instance
In the AWS Console, clickServices, then clickRelational Database Serviceto open theAmazon RDS dashboard.
In the left navigation pane, clickInstances. From the list of instances, selectedx-photos-db.
At the top, clickInstance actions, and then clickStart.
2. Download and explore the exercise code.
Type the command below in your AWS Cloud9 terminal to make sure you are in the ~/environment directory of your AWS Cloud9 instance.
cd ~/environment
In your AWS Cloud9 environment, download the exercise code by typing the command below in the terminal.
Unzip the exercise code .zip file by typing the command below in your AWS Cloud9 terminal.
unzip ex-xray.zip
The contents of the .zip file should be extracted to a folder with a similar name. You can view the folder on the left tree view.
Open theexercise-xray/FlaskApp/application.pyfile and explore the code. Notice that the AWS X-Ray libraries, such as aws_xray_sdk.core, xray_recorder, and XRayMiddleware, are now being imported in the application.py file. AWS X-Ray is configured with XRayMiddleware to gather tracing information for different services and report the information back into the AWS cloud.
Open theexercise-xray/util.py file. Notice that there is a@xray_recorder.capture()decorator, which captures the time needed to resize the uploaded photo to a desired size. This information is reported back to the AWS cloud.
3. Download and run the AWS X-Ray daemon on your AWS Cloud9 instance.
On the documentation page, scroll down until you see a link toLinux (executable)-aws-xray-daemon-linux-2.x.zip (sig). Right-click the link and copy the link address.
In your AWS Cloud9 instance terminal, type the command below to go to your home directory.
cd ~
Typewgetand paste the AWS X-Ray daemon hyperlink address that you copied. The command should look like the example below.
Unzip the AWS X-Ray daemon by typing the command below. Make sure that the name of the .zip file matches the one in the command below.
unzip aws-xray-daemon-linux-2.x.zip
Run the AWS X-Ray daemon by typing the command below.
./xray
4. Run the exercise code and test the application.
To run the exercise code, you will need to point the Run Configuration to the correct exercise folder. In thePython3RunConfigurationpane at the bottom, in theCommandtext box, type the text shown below and then clickRun.
exercise-xray/FlaskApp/application.py
A message like the one below should appear.
Running on http://0.0.0.0:5000/
Make sure to establish an SSH tunnel to your AWS Cloud9 instance. You may refer to the steps in the previous exercise for creating an SSH tunnel.
Openhttp://localhost:5000, log in to the application with your credentials, and upload a photo. ClickMy photosat the top-right corner of the application. You should see your uploaded photo.
Go to your AWS Cloud9 instance terminal and notice that the AWS X-Ray daemon is sending diagnostics information to your AWS account. You should see information as shown in the screenshot below.
5. Explore the diagnostics information collected by AWS X-Ray.
In the AWS Console, clickServices, then clickX-Rayto open theAWS X-Ray dashboard.
If you are using AWS X-Ray for the first time, you may have to clickGet Startedand then clickCancelto exit the wizard.
ClickService mapin the left navigation menu. The service map has information about AWS services communicating with each other and the processing time each service needs.
On the left side, clickTraces. You should see response times and time taken by each component in the application.
In theTrace listat the bottom, click the trace item where theMethodcolumn containsPOST. You should see timing information for all the calls made in your application, as shown in the screenshot below. This particular trace information should be about the POST made to the application to upload a photo. Notice the time needed for Amazon S3, Amazon RDS and Amazon Rekognition to process the photo. You can also see the time needed to resize the image against the resize_image label.
With this diagnostics information, you can now find out which component in your application is taking longer to process.
The code below will pick up the current X-Ray segment from the xray_recorder.
from aws_xray_sdk.core import xray_recorder
...
document = xray_recorder.current_segment()
Inside the homepage route, can you add an annotation to the current segment containing the user's nickname? When this is reported back to AWS Cloud9, you will be able to search traces by username.
Optional Challenge 2
AWS X-ray will capture and report exceptions. Can you add some code to create an exception - for example, a new route that simply divides a number by zero? Run this new code with X-Ray configured. You will see the stack trace captured and viewable in the X-Ray console.
6. Stop the Amazon RDS database instance.
In order to keep your AWS account bill to a minimum, consider stopping the Amazon RDS instance and then starting it again when needed. Follow the steps below to stop the Amazon RDS database instance.
In the AWS Console, go to the Amazon RDS dashboard.
In the left navigation pane, clickInstances. From the list of instances, selectedx-photos-db.
At the top, clickInstance actions, and then clickStop. A prompt will appear. ClickYes, stop now.
]]>deploy the application to two Amazon EC2 instanceshttp://www.tkk7.com/Noah/articles/433171.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 03:20:00 GMThttp://www.tkk7.com/Noah/articles/433171.htmlhttp://www.tkk7.com/Noah/comments/433171.htmlhttp://www.tkk7.com/Noah/articles/433171.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433171.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433171.html闃呰鍏ㄦ枃
]]>create an Amazon RDS database instancehttp://www.tkk7.com/Noah/articles/433170.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 03:18:00 GMThttp://www.tkk7.com/Noah/articles/433170.htmlhttp://www.tkk7.com/Noah/comments/433170.htmlhttp://www.tkk7.com/Noah/articles/433170.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433170.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433170.htmlIn this exercise, you will create an Amazon RDS database instance and store the Amazon S3 object key for the photo and photo labels in the database. This way, you are storing your data in a more structured format and making the application scalable. Note: Make sure to sign in to your AWS account with the AWS IAM useredXProjectUsercredentials.
To get started, follow the instructions below.
1. Create an Amazon RDS database instance.
In this section, you will create an Amazon RDS instance with the properties shown below to store photos and labels. If you are familiar with Amazon RDS, you may want to attempt to complete this section before reading the step-by-step instructions.
Region: Oregon (us-west-2) Amazon RDS Instance type: MySQL (free tier eligible) Name of DB instance: edx-photos-db Master username: master Master user password: Type a master user password and write it down for later use. VPC: edx-build-aws-vpc Database name: Photos Important: Make a note of the database endpoint.
In the AWS Console, clickServices, then clickRelational Database Serviceto open theAmazon RDS dashboard.
Make sure you are still in theOregonAWS Region.
On the left navigation menu, clickInstances.
ClickLaunch DB instance.
Scroll down to the bottom and select theOnly enable options eligible for RDS free usage tieroption.
Scroll back to the top and selectMySQL.
ClickNext.
Leave the default selections and scroll down toSettings.
ForDB instance identifier, typeedx-photos-db
ForMaster username, typemaster
Type apasswordfor the master user and confirm the password.Make a noteof the password for later use.
ClickNext.
In theNetwork & Securitysection, selectedx-build-aws-vpc.
Scroll down toDatabase options.
ForDatabase name, typePhotos.Make a noteof the database name for later use.
Leave the rest of the default settings, scroll down to the bottom and clickLaunch DB instance.
Note: It should take about five minutes for the instance to launch.
ClickView DB instance detailsto go to the DB instance details page.
After the instance launches, scroll down to theConnectsection and make a note of theEndpointfor later use. The endpoint will look like this:sample.cppyk3cpwnox.us-west-2.rds.amazonaws.com.
2. Modify the security group of the Amazon RDS database.
In this section, you will modify the security group of the Amazon RDS instance to the security group of the AWS Cloud9 instance.
On the Amazon RDS database instance page, scroll down to theDetailssection.
UnderSecurity and network, click the security group. The security group should have a name likerds-launch-wizard-xxx. A new page displaying the security group you just clicked should open.
Make a noteof the security group ID. You will need it in subsequent exercises.
On the bottom pane, clickInbound.
ClickEdit.
In theSourcetextbox, delete the existing text and typesg. A list of security groups will appear. Select the security group that contains your AWS Cloud9 environment name.
ClickSave.
3. Download the exercise code .zip file and unzip it to your AWS Cloud9 environment.
In this section, you will download the exercise code .zip file and unzip it to your AWS Cloud9 environment. If you feel familiar with the AWS Cloud9 environment from the previous few exercises, you may want to attempt to complete this section before reading the step-by-step instructions.
On the left tree view, you should see the exercise code .zip file namedex-rds.zip.
Unzip the exercise code zip file by typing the command below on your Cloud9 terminal.
unzip ex-rds.zip
The contents of the .zip file should be extracted to a folder with a similar name. You can view the folder on the left tree view.
4. Explore the exercise code.
In your AWS Cloud9 environment, on the left tree view, notice theexercise-rds/SetupScripts/database_create_tables.pyfile. This script creates the database tables needed for the application. The photo table stores the photos and labels information. The web_user is a restricted privilege user who has access solely to the photos table. The web application is configured to use the web_user and not the master user.
Open theexercise-rds/FlaskApp/database.pyfile and explore the code for adding a photo to the database and fetching it back from the database.
5. Run the database script.
To run thedatabase_create_tables.pyscript, type the command below in your AWS Cloud9 terminal window.
You should see a prompt on the screen to configure the database information. Follow the prompts and enter the information as shown below. Database host: Paste the database endpoint you noted earlier. Database user: master Database password: Type the password for the master user. Database name: Photos web_user password: Type a password for the web_user.Make a noteof the web_user password for later use.
You should see a message that a web_user is created with the required access granted to it.
6. Configure environment variables and run and test the code.
For the code to run successfully, you will need to configure the environment variables with the database details. Under theFlaskAppfolder, open theconfig.pyfile. Notice that the config.py file is now updated with the database-related environment variables.
In your AWS Cloud9 environment, on theRun Configurationpane at the bottom, clickENVon the right side. You should see a small list showing the previously configured environment variables.
To configure the database environment variables, type the environment variableNameandValueas shown in the table below:
Name
Value
DATABASE_HOST
Database endpoint you noted earlier
DATABASE_USER
web_user
DATABASE_PASSWORD
Password for the web_user you noted earlier
DATABASE_DB_NAME
Photos
Note: Make sure to delete any white space inserted while copy/pasting.
To run the code, you will need to point the Run Configuration to the correct exercise folder. OnPython3RunConfigurationpane at the bottom, in theCommandtextbox, type the text shown below and clickRunon the left side.
exercise-rds/FlaskApp/application.py
You should see a message like this one:
Running on http://0.0.0.0:5000
To test the code, go to your browser and type the IP address of the AWS Cloud9 environment. At the end of the IP address, type:5000 You should see the application running on port 5000.
ClickHomeand upload a photo. You should see the photo and the Amazon Rekognition labels generated for the photo.
ClickHome. You should see a table with the thumbnail for the photo and the label information. This information is being fetched from the database.
Try uploading a few more photos and watch as the table on the Home page is populated with the information saved in the database.
Optional Challenge
There is a command line mysql client on your Cloud9 instance.
The mysql client takes parameters for the database host and user. A "-p" switch tells the client to prompt you for a password. To connect to your RDS database, run the command below (replace DATABASE_HOST with your RDS database endpoint).
mysql -h DATABASE_HOST -u web_user -p
Can youSELECTthe contents of the photo table?
7. Stop the Amazon RDS database instance.
To keep your AWS account bill to a minimum, consider stopping the Amazon RDS instance and then starting it again when needed. Follow the steps below to stop the Amazon RDS database instance.
In the AWS Console, clickServices, then clickRelational Database Serviceto open theAmazon RDS dashboard.
In the left navigation pane, clickInstances. From the list of instances, selectedx-photos-db.
At the top, clickInstance actions -> Stop. You will get a prompt. ClickYes, stop now.
]]>adding the Amazon Rekognition componenthttp://www.tkk7.com/Noah/articles/433169.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 03:16:00 GMThttp://www.tkk7.com/Noah/articles/433169.htmlhttp://www.tkk7.com/Noah/comments/433169.htmlhttp://www.tkk7.com/Noah/articles/433169.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433169.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433169.htmlIn this exercise, you will extend the application by adding the Amazon Rekognition component. As soon as you upload a photo to your Amazon S3 bucket, Amazon Rekognition processes the photo and identifies objects, people, text, scenes, and activities in the photo and labels it accordingly. Note: Make sure to sign in to your AWS account with the AWS IAM useredXProjectUsercredentials.
To get started, follow the instructions below.
1. Download the exercise code .zip file to your AWS Cloud9 environment.
Type the command below in your AWS Cloud9 terminal to make sure you are in the ~/environment directory of your AWS Cloud9 instance.
cd ~/environment
Type the command below in your AWS Cloud9 terminal to download the python code for the Amazon Rekognition component.
On the left tree view, you should see the exercise code .zip file namedex-rekognition.zip.
2. Unzip the exercise code .zip file.
Unzip the exercise code .zip file by typing the command below on your AWS Cloud9 terminal.
unzip ex-rekognition.zip
The contents of the .zip file should be extracted to a folder with a similar name. You can view the folder on the left tree view.
You may want to close any tabs that remain open from previous exercises.
3. Explore the exercise code.
Open theexercise-rekognition/FlaskApp/application.pyfile.
In theHomepage route function, notice that a Boto 3 client for Amazon Rekognition is created. The image uploaded in the Amazon S3 bucket is passed to the detect_labels API, which returns a list of labels processed by Amazon Rekognition. These labels are then populated on the UI.
4. Run and test the code.
To run the application.py code, on the top menu bar, clickRun -> Run Configurations -> Python3RunConfiguration.
Important: Notice that the run configuration runs the application.py for the previous exercise.
Point the run configuration to the correct exercise folder by editing the folder path in theCommandtext box in the bottom pane. In that text box, typeexercise-rekognition/FlaskApp/application.py
ClickRunon the left side. You should see a message like this:
Running on http://0.0.0.0:5000/
Go to your browser and type the IP address of the Amazon EC2 instance that hosts your AWS Cloud9 environment. At the end of the IP address, type:5000
The application should now have the functionality related to Amazon Rekognition.
To test the Amazon Rekognition component, clickHomeon the application.
Upload an image. Amazon Rekognition should label the image with the image properties.
Optional Challenge
The Boto 3detect_labelsresponse includes aConfidencevalue. Can you update the application UI to include theConfidence? Or define a threshold and only display labels over the confidence threshold?
]]>create AWS Cloud9 environmenthttp://www.tkk7.com/Noah/articles/433168.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 03:15:00 GMThttp://www.tkk7.com/Noah/articles/433168.htmlhttp://www.tkk7.com/Noah/comments/433168.htmlhttp://www.tkk7.com/Noah/articles/433168.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433168.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433168.htmlIn this exercise, you will create your first AWS Cloud9 environment and start building the first component of the course project. AWS Cloud9 is a cloud-based IDE that lets you write, run, and debug your code with just a browser. Note: Make sure to sign in to your AWS account with the AWS IAM useredXProjectUsercredentials.
Architecture Diagram
The diagram shown above is the architecture diagram for the application you will build over the duration of the course. The course project is a simple web application in which users log in to their accounts and upload photos to an Amazon S3 bucket, process the photos with Amazon Rekognition to generate labels and then store the photos along with their labels and description in an Amazon RDS database.
The application is deployed on an Amazon EC2 instance with an Application Load Balancer sitting in front of the instance to direct user requests to the instance. Amazon Cognito is used to sign up/sign in users for the application. In order to asynchronously process the photo labels, when a photo is uploaded, an Amazon S3 bucket event notification is issued to an Amazon SNS topic. This triggers a subscribed AWS Lambda function, which talks to Amazon Rekognition. To make the application more distributed, an Amazon SQS queue subscribed to the Amazon SNS topic stores all the incoming requests and an on-premises application polls the queue for processing. AWS X-Ray traces the calls made to all the AWS resources in this application, thereby providing diagnostics information. The application is coded in Python 3 using AWS Cloud9 as the IDE.
In this exercise, you will build the Amazon S3 uploader component of the application. In this component, users will upload a photo to an Amazon S3 bucket.
To begin, follow the instructions below.
1. Create AWS Cloud9 environment and explore the environment.
In this section, you will create an AWS Cloud9 environment and explore the environment.
In the AWS Console, clickServices, then clickCloud9to open theCloud9 dashboard.
Make sure you are in theOregonregion.
ClickCreate environmentat the top-right corner.
ForName, typeBuildingOnAWS
ClickNext step.
On theConfigure settingspage, leave the default selection in theEnvironment settingssection and scroll down toNetwork settings (advanced).
ForNetwork (VPC), choose theedx-build-aws-vpc idyou wrote down in the third exercise.
ForSubnet, choose thepublic subnetin edx-build-aws-vpc with the subnet-id you noted in the third exercise.
ClickNext step.
Review the details and clickCreate environment. This should launch your AWS Cloud9 environment in a few minutes.
Upon environment creation, notice the terminal window on the bottom pane. The terminal provides a remote login to the instance on which the AWS Cloud9 environment is hosted, just as you used SSH for remote login in the third exercise. A pre-authenticated AWS CLI is installed in your terminal.
Explore the terminalby typing this command:
aws ec2 describe-instances
This should give a JSON output with the all the information of the EC2 instances in your account.
InstallBoto 3on your AWS Cloud9 instance by typing this command:
sudo pip-3.6 install boto3
At the terminal, typepython3and press ENTER.
Explore the Python Boto 3 APIs by executing these commands:
2. Create an Amazon S3 bucket to store the application photos.
In this section, you will create an Amazon S3 bucket in your AWS account. This bucket will store your application photos. If you are familiar with Amazon S3, you may want to attempt to complete this section by using the properties below before reading the step-by-step instructions.
Region: Oregon (us-west-2) Bucket name: Type a unique bucket name to store photos and make a note of it for later use.
In the AWS Console, clickServices, then clickS3to open theS3 dashboard.
ClickCreate bucket.
ForBucket name, type a unique bucket name to store photos. Write down the name of the bucket for later use.
ForRegion, make sure you have selected theOregon (us-west-2)region.
ClickCreate.
3. Build the Amazon S3 uploader component of the application in the AWS Cloud9 environment.
In this section, you will build the Amazon S3 uploader component of the application. The Amazon S3 uploader component uploads photos to the Amazon S3 bucket you created in the previous section.
Type the command below in your AWS Cloud9 terminal to make sure you are in the ~/environment directory of your AWS Cloud9 instance.
cd ~/environment
Unzip the exercise code .zip file by typing the command below in your AWS Cloud9 terminal.
unzip ex-s3-upload.zip
The contents of the .zip file should be extracted to a folder with a similar name. You can view the folder on the left tree view.
Open theexercise-s3-upload/FlaskApp/application.pyfile and review the code. Notice that an Amazon S3 client has been created to interact with Amazon S3 via the Boto 3 API.
Install the requirements for the project by executing the command below in your AWS Cloud9 terminal.
Selectexercise-s3-upload/FlaskApp/application.pyin the tree view.
On the top menu bar, clickRun -> Run With -> Python 3. Ensure that you are using the Python 3 runner, as shown in the screenshot below.
You should see an error message asking for an environment variable named PHOTOS_BUCKET.
To successfully run the code, you will need to set environment variables. To do so, follow the instructions below:
On the right side of theRun Configurationpane at the bottom, clickENV, as in the screenshot below.
In theNamecolumn, typePHOTOS_BUCKET
In theValuecolumn, type the name of the Amazon S3 bucket you created earlier. Note: Make sure to delete any white space that was inserted while copy/pasting.
In theNamecolumn, typeFLASK_SECRET The Flask app uses a secret value to encrypt session variables.
In theValuecolumn, type random characters and numbers.
Save the run configuration by typing a name for the configuration. This saves the run configuration with all the environment variables and the runner information. In the text box next to theRunbutton, typePython3RunConfiguration, as shown in the screenshot below.
Run application.py again. This time, it should run successfully. In the terminal, you will see a message like this: Running on http://0.0.0.0:5000/
That means the Python Flask app is now running on port 5000 of the AWS Cloud9 instance.
To verify that the app is running, you will need to edit the security group of the AWS Cloud9 instance. Go to the EC2 dashboard, locate your AWS Cloud9 instance, and select it.
In theDescriptionpane at the bottom, click the security group hyperlink. This should open up the security group associated with the AWS Cloud9 instance.
On the bottom pane, clickInbound.
ClickEdit.
ClickAdd Rule.
ForPort Range, type5000
ForSource, type0.0.0.0/0
ClickSave.
Return to your AWS Cloud9 environment. On the top-right corner, clickShare, as shown in the screenshot below.
Copy the IP address displayed in front ofApplication. This is the public IP address of the AWS Cloud9 instance.
Go to your browser and type the IP address of the AWS Cloud9 instance followed by:5000The uploaded component of the project is now up and running!
Try uploading a few photos via the app. You will notice that the photos are stored in the Amazon S3 bucket that you created earlier. Note: Your AWS Cloud9 instance has been configured to auto-hibernate your environment to prevent unnecessary charges.
Optional Challenge
The Boto 3list_objectsmethod returns a LastModified property. Can you update the application to display photos in LastModified order?
]]>create AWS IAM userhttp://www.tkk7.com/Noah/articles/433167.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 03:12:00 GMThttp://www.tkk7.com/Noah/articles/433167.htmlhttp://www.tkk7.com/Noah/comments/433167.htmlhttp://www.tkk7.com/Noah/articles/433167.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433167.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433167.htmlAs you learned in the lecture, you should not use your AWS account root user credentials to access AWS. Instead, create an AWS IAM user and assign permissions only necessary for the work done by the user. In this exercise, you will create an AWS IAM user, attach a customer managed AWS IAM policy to the user and set up access keys for the AWS IAM user.
An AWS IAM user is an entity that you create in AWS to represent the person or service that uses it to interact with AWS. You attach permission policies to the IAM user that determine what the user can and cannot do in AWS.
Access keys are a combination of an access key ID and a secret access key that are assigned to a user. These can be used to make programmatic calls to AWS when using the API in program code or at a command prompt when using the AWS CLI.
For all subsequent exercises, make sure to log in with the AWS IAM user credentials you create in this exercise, rather than the root user credentials.
You will also create an Amazon EC2 instance, SSH into the instance, and configure AWS CLI to explore the AWS CLI commands. Then you will install Boto 3 on the instance and try out some Python scripting on the terminal. Boto 3 is the AWS SDK for Python, making it easier to integrate your Python application, library, or script with AWS services.
To begin, follow the instructions below.
1. Create an AWS IAM policy.
In this section, you will create an AWS IAM customer-managed policy. Customer-managed policies provide more precise control over your policies than AWS managed policies. This policy will have permissions specific to the AWS resources needed for the application you will build in this course.
In the AWS Management Console, clickServices, then clickIAMto open theIAM dashboard.
In the left navigation menu, clickPolicies.
ClickCreate policy.
Click theJSONtab.
In the editor textbox, completely replace the sample policy with the following.
You have successfully created an AWS IAM policy with full access to AWS IAM, Amazon EC2, Amazon S3, Amazon RDS, Amazon SNS, Amazon SQS, Amazon Rekognition, AWS Lambda, Amazon Cognito, AWS Cloud9, AWS X-Ray, and AWS CloudFormation. When you create IAM policies, follow the standard security advice of grantingleast privilege- that is, granting only the permissions required to perform a task. Determine what users need to do and then craft policies for them that let the users perform only those tasks.
2. Create an AWS IAM user, attach a policy to the user, and generate access keys.
In this section, you will create an AWS IAM user and attach the policy you just created to the user. You will then generate the access keys for the user. Those access keys will be used to make programmatic calls to AWS services via AWS CLI or APIs. If you are familiar with AWS IAM users, you may want to attempt to complete this section before reading the step-by-step instructions.
AWS IAM user name: edXProjectUser Access type: Programmatic access and AWS Management Console access Policy: edXProjectPolicy Important:Download the.csv filewith the access keys after creating the user. Also, make sure to click theSend emaillink to get the email instructions for signing in to the AWS Management Console as edXProjectUser.
Reminder!Be sure to protect your AWS account access keys like you would your credit card numbers or any other sensitive secret.
At the end of this exercise, you will not be using the access keys again. It is a security best practice to remove IAM user credentials that are not needed. After this exercise, make sure to remove the access keys only (not the AWS Console password) for the IAM user - edXProjectUser. See moreIAM Best Practices.
In the AWS Console, clickServices, then clickIAMto go to theIAM dashboard.
In the left navigation menu, clickUsers.
ClickAdd user.
In theUser nametext box, typeedXProjectUser
ForAccess type, selectProgrammatic access and AWS Management Console access.
ForConsole password, you may choose eitherAutogenerated passwordorCustom password. If you choose Autogenerated, you will be prompted to change your console password when you log in to the AWS Console as the edXProjectUser user. Make sure you take a note of the password created.
ClickNext: Permissions.
UnderSet permissions for edXProjectUsersection, clickAttach existing policies directly.
In the search text box forFilter, typeedXProjectPolicy. SelectedXProjectPolicyfrom the filtered list.
ClickNext: Review.
Review the information and clickCreate user. You should see a success message.
ClickDownload .csvto download the access key ID and secret access key. Note: This is your only chance to download these credentials.
In theEmail login instructionscolumn, clickSend email. You can send an email to an email address of your choice. This email contains the instructions to sign in to your AWS account with the edXProjectUser AWS IAM user credentials.
ClickCloseto return to the console.
In the left navigation menu, clickDashboard.
Note the IAM users sign-in link. This is a special URL for IAM users, which includes your account ID. You will see the same URL in the email you just created.
Sign out of the console, and follow the instructions provided in the email you just received to sign in to the AWS Console as the edXProjectUser AWS IAM user.
3. Create an Amazon EC2 instance and configure AWS CLI with the access keys of the AWS IAM user edXProjectUser.
Sign-in to your AWS account as theedXProjectUserAWS IAM user.
Create an Amazon EC2 instance using the properties below. If you are familiar with Amazon EC2, you may want to attempt to complete this portion before reading the step-by-step instructions.
Region: Oregon (us-west-2) Amazon Machine Image (AMI): Amazon Linux AMI Instance Type: t2.micro Network VPC: edx-build-aws-vpc Subnet: edx-subnet-public-a Tag: Ex4WebServer Security group name: Use the security group created in the third exercise, exercise3-sg. Key Pair: Use the key pair created in the third exercise.
In the AWS Console, clickServices, then clickEC2to go to theEC2 dashboard.
Make sure you are in theOregonregion.
From the EC2 dashboard, clickLaunch Instance.
On theChoose an Amazon Machine Image (AMI)page, select theAmazon Linux AMI. This AMI is free-tier eligible.
On theChoose an Instance Typepage, selectt2.micro.
ClickNext: Add Storage. Skip through this page and clickNext: Add Tags.
ClickAdd Tag.
In the Key textbox, typeName.
In the Value textbox, typeEx4WebServer.
ClickNext: Configure Security Group. Select theSelect an existing security groupoption.
From the list of security groups, selectexercise3-sg.
ClickReview and Launch.
On the Review Instance Launch page, review the details and clickLaunch.
When prompted for a key pair, selectChoose an existing key pair, and then choose the key pair you created in the third exercise.
Select the acknowledgement check box, and then clickLaunch Instances.
ClickView Instancesto return to the instances page.
On the Instances page, you can view the status of the launch. It can take a few minutes for the instance to be ready so that you can connect to it. Check that your instance has passed its status checks. You can view this information in the Status Checks column.
Once the instance is ready, select the instance and note down theIPv4 Public IPfound in theDescriptionstab at the bottom.
Connect to the instance using SSH. You may refer to the instructions in thethird exercisefor connecting to the instance.
Open thecredentials.csvfile that you downloaded earlier. Find the entry foredXProjectUser, and note the values forAccess Key IdandSecret Access Key.
On the instance terminal, type the below command.
aws configure
Follow the prompts on the screen and paste in the values forAccess Key IdandSecret Access Key.
ForRegion, typeus-west-2.
ForDefault output format, press ENTER. You have now configured the AWS CLI so that any CLI calls will operate with the credentials of the AWS IAM user edXProjectUser.
Now query the information about the Amazon EC2 instances in your account. Type the command below.
aws ec2 describe-instances
You should see a JSON output with all the information of the Amazon EC2 instances in your account. This means that you were able to successfully execute the AWS CLI command with the permissions attached to the edXProjectUser.
4. Install Boto 3 on the instance and explore Boto 3 APIs.
First install Python 3 and the Boto 3 SDK. On the Amazon EC2 instance terminal, type the commands below.
To start using Boto 3, typepython3on the instance terminal and press ENTER. You should now be able to execute Python commands from your instance terminal.
Import boto3 and create a client for the corresponding AWS service you wish to use. In this case, you can explore the EC2 APIs for Boto 3 by creating the EC2 client. Type the following.
import boto3 client = boto3.client('ec2') client.describe_instances()
You should see a JSON output similar to the one given by the AWS CLI command.
Now, type the command below.
client.describe_key_pairs()
You should see a JSON output with the information about the key pairs in your account.
PressCtrl-Dto exit the python interpreter.
Optional Challenge
In this exercise, you configured an access key (access key ID and secret access key) on your EC2 instance. Later in the course, we will introduce IAMroles. You may want to read ahead a little, and look atIAM Roles for Amazon EC2.
Can you see a way to complete this exercise using an IAM role on the instance, rather than the access keys you just used?
5. Terminate the Amazon EC2 instance.
In this section, you will terminate the Amazon EC2 instance by selecting the instance in the Amazon EC2 dashboard and clickingActions -> Instance State -> Terminate.
In the console, clickServices, then clickEC2to open theEC2 dashboard.
In the navigation pane, clickInstances. In the list of instances, select theEx4WebServerinstance.
ClickActions,Instance State,Terminate.
ClickYes, Terminatewhen prompted for confirmation.
Amazon EC2 shuts down and terminates your instance. After your instance is terminated, it remains visible on the console for a short while, and then the entry is deleted.
]]>creating a VPC and subnets via an AWS CloudFormation templatehttp://www.tkk7.com/Noah/articles/433166.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 03:10:00 GMThttp://www.tkk7.com/Noah/articles/433166.htmlhttp://www.tkk7.com/Noah/comments/433166.htmlhttp://www.tkk7.com/Noah/articles/433166.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433166.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433166.htmlIn this exercise, you will start laying the infrastructure for the course project by creating a VPC and subnets via an AWS CloudFormation template. You will create more AWS resources in these subnet in the subsequent exercises. You will also create an Amazon EC2 instance and learn how to connect to it using an SSH client. You use an SSH client for remote login and administration over an authenticated secure channel. You will then look at the log files on the instance and extract information from the instance metadata and user data script.
To get started, follow the steps below.
1. Launch an AWS CloudFormation template to create VPC and subnets.
In this section, you will create a VPC and subnets by launching an AWS CloudFormation template. If you are familiar with AWS CloudFormation, you may want to attempt to complete this section by using the properties below before reading the step-by-step instructions.
Region: Oregon (us-west-2) CloudFormation template:Download template Name of the stack: edx-vpc-stack
In the AWS Console, clickServices, then clickCloudFormationto open theCloudFormation dashboard.
To select the AWS CloudFormation template you just downloaded, clickChoose file.
ClickNext.
In theStack nametextbox, typeedx-vpc-stack.
ClickNext. Skip the Options page and clickNext.
ClickCreate. You will notice that the status of the template isCREATE_IN_PROGRESS.The template should finish creating in a minute.
In your AWS Management Console home page, in theAWS servicessearch textbox at the top, typeVPC, and then selectVPCto open the VPC dashboard.
In the VPC dashboard, on the left navigation menu, clickYour VPCs. You will see a VPC namededx-build-aws-vpcin the list. Write down the vpc-id of the edx-build-aws-vpc. You will need the vpc-id in subsequent exercises.
In the VPC dashboard, on the left navigation menu, clickSubnets. You will see four subnets starting with edx-subnet-xxxx. Write down the subnet-id of edx-subnet-public-a. You will need the subnet-id in subsequent exercises.
2. Launch an Amazon EC2 instance with a user data script in a VPC.
In this section, you will launch an Amazon EC2 instance with an user data script. If you are familiar with Amazon EC2, you may want to attempt to complete this section by using the properties below before reading the step-by-step instructions.
Region: Oregon (us-west-2) Amazon Machine Image (AMI): Amazon Linux AMI Instance Type: t2.micro Network VPC: edx-build-aws-vpc Subnet: edx-subnet-public-a User data script:Download Tag: Ex3WebServer Security group name: exercise3-sg Security group rules: Allow HTTP and SSH Key Pair: Create a new key pair and save it for later use.
In the AWS Console, clickServices, then clickEC2to open theEC2 dashboard.
At the top right corner, select theUS West (Oregon)region.
From the EC2 dashboard, clickLaunch Instance.
On theChoose an Amazon Machine Image (AMI)page, selectAmazon Linux AMIby clickingSelect. This AMI is free-tier eligible.
On theChoose an Instance Typepage, selectt2.micro.
ClickNext: Configure Instance Details.
ForNetwork, selectedx-build-aws-vpc.
ForSubnet, selectedx-subnet-public-a.
Leave the defaults and scroll down to theAdvanced Detailssection and expand it.
ClickNext: Add Storage. Skip through this page and clickNext: Add Tags.
ClickAdd Tag.
In theKeytextbox, typeName
In theValuetextbox, typeEx3WebServer
ClickNext: Configure Security Group. Note that the wizard gives you an option to create a new security group or select an existing one. For this exercise, leave the default chosen option, Create a new security group.
ForSecurity group name, typeexercise3-sg
ClickAdd Rule.
ForType, leaveCustom TCP Ruleselected.
ForPort Range, type80
ForSource, type0.0.0.0/0
Note: The inbound rule for SSH is added by default.
ClickReview and Launch.
On theReview Instance Launchpage, review the details and clickLaunch.
When prompted for a key pair, selectCreate a new key pair, enter a name for the key pair, and then clickDownload Key Pair.
Note: This is the only chance for you to save the private key file, so be sure to download it. You will use the same key pair for all subsequent exercises in the course. Save the private key file in a safe place. You'll need to provide the name of your key pair when you launch an instance and the corresponding private key each time you connect to the instance.
Select the acknowledgement check box, and then clickLaunch Instances.
ClickView Instancesto return to the instances page.
On theInstancespage, you can view the status of the launch. It can take a few minutes for the instance to be ready so that you can connect to it. Check that your instance has passed its status checks. You can view this information in theStatus Checkscolumn. Note: It takes a few minutes for the status checks to pass. Wait until the status checks changes from Initializing to 2/2 checks passed.
Once the instance is ready, select the instance and note down theIPv4 Public IPfound in theDescriptionstab at the bottom.
Paste the public IP address of your instance in your web browser to display the welcome web page. This page is now displaying dynamic information about your server.
3. Connect to your Amazon EC2 instance.
In this section, you will connect to your Amazon EC2 instance via SSH. An SSH connection requires port 22 to be open on your network. You may need to contact your network administrator to ensure that this is open.
For MAC/Linux users:
Open theTerminalapplication.
Type the commands below. In both commands, replacePATH-TO-PEM-FILEwith a reference to the .pem file that you downloaded while launching the instance. In the second command, replacePUBLIC-IPwith the IPv4 Public IP of the instance.
You will see a prompt like the one below. Answeryesto the prompt.
The authenticity of host '54.201.7.240 (54.201.7.240)' can't be established. ECDSA key fingerprint is SHA256:TrCPkFBL0F+pTp3LH+UGFPhGjl7N4qaoLucu21RWsRM. Are you sure you want to continue connecting (yes/no)?
PuTTY does not natively support the private key format (.pem) generated by Amazon EC2. PuTTY has a tool named PuTTYgen, which can convert keys to the required PuTTY format (.ppk). You must convert your private key into this format (.ppk) before attempting to connect to your instance using PuTTY. To convert your private key (.pem file) using PuTTYgen, follow the instructions below:
Start PuTTYgen.
UnderType of key to generate, selectRSA.
ClickLoad. By default, PuTTYgen displays only files with the extension .ppk. To locate your .pem file, select the option to display files of all types.
Select your.pem filefor the key pair that you specified when you launched your instance, and then clickOpen. ClickOKto dismiss the confirmation dialog box.
ClickSave private keyto save the key in the format that PuTTY can use. PuTTYgen displays a warning about saving the key without a passphrase. ClickYes.
Specify the same name for the key that you used for the key pair (for example, my-key-pair). PuTTY automatically adds the .ppk file extension. Your private key is now in the correct format for use with PuTTY. You can now connect to your instance using PuTTY's SSH client.
StartPuTTY.
In theCategorypane, clickSession.
In theHost Nametext box, typeec2-user@IP-ADDRESS, where IP-ADDRESS is the public IP address of your Amazon EC2 instance.
In theCategorypane, expandConnection, expandSSH, and then clickAuth.
ClickBrowse.
Select the.ppk filethat you generated for your key pair, and then clickOpen.
If you plan to start this session again later, you can save the session information for future use. In theCategorytree, selectSession, enter a name for the session inSaved Sessions, and then clickSave.
ClickOpento start the PuTTY session. PuTTY will ask whether you wish to cache the server’s host key. ClickYes.
4. View log file, query instance metadata, and user data.
In this section, you will inspect the cloud-init logs to verify the steps in the UserData script executed on the Amazon EC2 Instance. Then, you will query the instance metadata service from the Amazon EC2 instance so that you can see how your application is printing information about itself on the welcome page.
To view the log file, type the command below in your instance terminal.
cat /var/log/cloud-init-output.log
Explore the log file to see the log entries generated for installing the user data script.
To view the instance metadata, type the command below:
curl http://169.254.169.254/latest/meta-data/
Execute the command below to get the instance identity document of your instance:
Execute the command below to get the MAC address of the instance:
curl http://169.254.169.254/latest/meta-data/mac
Execute the command below to get the VPC ID in which the instance resides. Make sure to replaceYour-MACin the command below with the MAC address of your instance:
Execute the command below to get the subnet-id in which the instance resides. Make sure to replaceYour-MACin the command below with the MAC address of your instance:
Execute the command below to get the instance user data:
curl http://169.254.169.254/latest/user-data
Go to theEC2 dashboardin your console, locate the Amazon EC2 instance you created and verify the public IP address, the VPC ID and the subnet-id of the instance you just queried in the instance terminal. You should be able see this information in theDescriptiontab at the bottom.
5. Terminate the Amazon EC2 instance.
In this section, you will terminate the Amazon EC2 instance by selecting the instance in the EC2 dashboard and clickingActions -> Instance State -> Terminate.
In the AWS Console, clickServices, then clickEC2to open theEC2 dashboard.
In the navigation pane, clickInstances. In the list of instances, select theEx3WebServerinstance.
ClickActions,Instance State,Terminate.
ClickYes, Terminatewhen prompted for confirmation.
Amazon EC2 shuts down and terminates your instance. After your instance is terminated, it remains visible on the console for a short while, and then the entry is deleted.
]]>create Amazon EC2http://www.tkk7.com/Noah/articles/433165.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 02:59:00 GMThttp://www.tkk7.com/Noah/articles/433165.htmlhttp://www.tkk7.com/Noah/comments/433165.htmlhttp://www.tkk7.com/Noah/articles/433165.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433165.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433165.htmlIn this exercise, you will create your first Amazon EC2 instance and install a sample Python Flask app using user data. When you launch an instance in Amazon EC2, you can pass user data to the instance that can be used to perform common automated configuration tasks. User data is usually passed in the form of shell-scripts. You can pass this data into the launch wizard as plain text, as a file while launching via the command line tools, or as base64-encoded text for API calls.
To get started, follow the steps below.
1. Launch an Amazon EC2 instance with a user data script.
In this section, you will launch an Amazon EC2 instance with a user data script. If you are familiar with Amazon EC2, you may want to attempt to complete this section by using the properties below before reading the step-by-step instructions.
Region: Oregon (us-west-2) Amazon Machine Image (AMI): Amazon Linux AMI Instance Type: t2.micro User data script:Download Tag: SamplePythonFlaskApp Security group name: exercise2-sg Security group rules: Allow HTTP Key Pair: Proceed without a key pair
In the AWS Console, clickServices, then clickEC2to open theEC2 dashboard.
At the top-right corner, select theUS West (Oregon)region.
From the EC2 dashboard, clickLaunch Instance.
On theChoose an Amazon Machine Image (AMI)page, selectAmazon Linux AMIby clickingSelect. This AMI is free-tier eligible. Note: Do not select the Amazon Linux 2 AMI option.
On theChoose an Instance Typepage, you can select the hardware configuration of your instance. Selectt2.micro.
ClickNext: Configure Instance Details.
On the Configure Instance Details page, leave the defaults and scroll down to theAdvanced Detailssection and expand it.
ClickNext: Add Storage. Skip through this page and clickNext: Add Tags.
ClickAdd Tag. Tags enable you to categorize your AWS resources in different ways - for example, by purpose, owner, or environment.
In the Key textbox, typeName
In the Value textbox, typeSamplePythonFlaskApp
ClickNext: Configure Security Group. Note that the wizard gives you an option to create a new security group or select an existing one. For this exercise, accept the default chosen option, Create a new security group.
For Security Group Name, typeexercise2-sg
In the security group table,delete the SSH ruleby clicking theXbutton at the end of the row.
ClickAdd Rule.
ForType, leaveCustom TCP Ruleselected.
ForPort Range, type80
ForSource, type0.0.0.0/0
ClickReview and Launch.
On the Review Instance Launch page, review the details and clickLaunch.
When prompted for a key pair, selectProceed without a key pair.
Select the acknowledgement check box, and then clickLaunch Instances.
ClickView Instancesto return to the Instances page.
On the Instances page, you can view the status of the launch. It can take a few minutes for the instance to be ready so that you can connect to it. Check that your instance has passed its status checks. You can view this information in the Status Checks column. Note: It takes a few minutes for the status checks to pass. Wait until the status checks changes fromInitializingto2/2 checks passed.
Once the instance is ready, select the instance and write down theIPv4 Public IPfound in the Descriptions tab at the bottom.
2. Test the sample app running on your instance
Open a browser and type the public IP of the Amazon EC2 instance you copied earlier.
You should see a sample Python app running on your Amazon EC2 instance. Congratulations!You have launched your first web server in AWS.
3. Terminate the Amazon EC2 instance
In this section, you will terminate the Amazon EC2 instance by selecting the instance in EC2 dashboard and clickingActions -> Instance State -> Terminate.
In the AWS Console, clickServices, then clickEC2to open theEC2 dashboard.
In the navigation pane, clickInstances. In the list of instances, select theSamplePythonFlaskAppinstance.
ClickActions,Instance State,Terminate.
ClickYes, Terminatewhen prompted for confirmation.
Amazon EC2 shuts down and terminates your instance. After your instance is terminated, it remains visible on the console for a short while, and then the entry is deleted.
]]>create and activate AWS accounthttp://www.tkk7.com/Noah/articles/433164.html鐪奸暅铔?/dc:creator>鐪奸暅铔?/author>Thu, 19 Apr 2018 02:49:00 GMThttp://www.tkk7.com/Noah/articles/433164.htmlhttp://www.tkk7.com/Noah/comments/433164.htmlhttp://www.tkk7.com/Noah/articles/433164.html#Feedback0http://www.tkk7.com/Noah/comments/commentRss/433164.htmlhttp://www.tkk7.com/Noah/services/trackbacks/433164.htmlIn this exercise, you will create and activate your AWS account. To begin, follow the steps below.
On the sign-in page, clickCreate an Account. Note: This might be unavailable in your browser if you previously signed in to the AWS Management Console. In that case, clickSign in to a different account, and then clickCreate a new account.
Type the requested account information, and then clickContinue.
ChoosePersonalorProfessional.
Note:These two account types are identical in functionality. You can choose a personal account for your personal projects or choose professional for use within your company, an educational institution, or an organization.
Type the requested company or personal information.