If you know a column is going to have heavy skew, you can specify this in the table's schema, for example: CREATE TABLE Customers ( id int, username string, zip int ) SKEWED BY (zip) ON (57701, 57702) STORED as DIRECTORIES;
By specifying the values with heavy skew, Hive will split those out into separate files automatically and take this fact into account during queries so that it can skip whole files if possible.
In the Customers table above, records with a zip of 57701 or 57702 will be stored in separate files because the assumption is that there will be a large number of customers in those two ZIP codes.
Examine the properties of table in the detailed table information obtained from DESCRIBE FORMATTED Customers command.
Update Security Groups Automatically Using AWS Lambda
Update Security Groups Automatically Using AWS Lambda
Lab Overview
Overview
Security is a top priority for Amazon Web Services (AWS). AWS provides many tools and services to meet your unique security needs. This lab will present a solution to enhance your security (one of many). The lab walks you through a method to automatically update your Virtual Private Cloud (VPC) Security Groups to only allow access from Amazon CloudFront and AWS Web Application Firewall (WAF). Defining Security Groups rules this way prevents malicious requests from by-passing AWS WAF security rules and accessing your EC2 instances directly.
To only allow traffic that originates from Amazon CloudFront and AWS WAF's IP range, you need to be informed of AWS IP changes. AWS notifies users of service IP changes through a public Simple Notification Service topic that gives service IP ranges in json format. Leveraging the integration between Amazon SNS and AWS Lambda, this lab demonstrates a way to automatically update security groups with these new IPs.
Topics Covered
After completing this lab, you should be able to:
Create VPC Security Groups
Create IAM Policy
Create a Lambda function
Test Lambda function with sample events
Subscribe Lambda function to SNS topic
Technical knowledge prerequisites
This lab is intended for AWS learners. To successfully complete this lab, you should be familiar with AWS Services including Amazon EC2, VPC Security Groups, Identify and Access Management (IAM) Roles and Policies and Amazon Simple Notification Service (SNS). You should be comfortable logging into and using the AWS Management Console.
What is AWS Lambda?
Lambda is a compute service that provides resizable compute capacity in the cloud to make web-scale computing easier for developers. You can upload your code to AWS Lambda and the service can run the code on your behalf using AWS infrastructure. AWS Lambda supports multiple coding languages: Node.js, Java, or Python.
After you upload your code and create a Lambda function, AWS Lambda takes care of provisioning and managing the servers that you use to run the code. In this lab, you will use AWS Lambda as a trigger-driven compute service where AWS Lambda runs your code in response to changes to an Amazon EC2 security group. The code for the Lambda function will be provided with this lab.
What Is Amazon CloudFormation?
AWS CloudFormation gives developers and system administrators an easy way to create and manage a collection of related AWS resources, provisioning and updating them in an orderly and predictable fashion.
You can use the AWS CloudFormation sample templates or create your own templates to describe the AWS resources, and any associated dependencies or runtime parameters, required to run your application. You don't need to figure out the order for provisioning AWS services or the subtleties of making those dependencies work. AWS CloudFormation takes care of this for you.
You can deploy and update a template and its associated collection of resources (called a stack) by using the AWS Management Console, AWS Command Line Interface, or APIs. AWS CloudFormation is available at no additional charge, and you pay only for the AWS resources needed to run your applications.
Create a security group
You're now going to create a security group in the AWS Management Console. This security group's ingress rules will be updated automatically by a Lambda function that you'll create subsequently to allow only the IP ranges belonging Amazon CloudFront and AWS WAF.
6.In the AWS Management Console, clickServices, then clickEC2.
7.In the navigation pane, clickSecurity Groups.
8.ClickCreate Security Group.
9.ForSecurity group name, type
NoteCopy the name to your clipboard as you will need it later.
10.ForDescription, type
11.ForVPC, chooseDefault VPC.
12.ClickCreate.
13.Now select the Security Group you created.
14.ClickActions, and then clickAdd/Edit Tags.
15.ClickCreate Tag.
Values are case sensitive. You will create a Lambda function that targets Security Groups with these tags to update security group rules.
16.Create two tags with the following values:
Key:
Value
Then create another tag:
Key:
Value:
17.ClickSave.
Update IAM role for the Lambda function
When creating a Lambda function, it's important to understand and properly define the security context to which the Lambda function is subject.
An IAM role has already been created for you as part of the lab setup. In this section, you will create an IAM policy with the permissions needed for the Lambda function to execute and attach that to the existing IAM role.
Create an IAM policy
NoteYou can ignore any warnings you may see.
18.In the AWS Management Console, on theServicesmenu clickIAM.
19.In the navigation pane, clickPolicies.
20.ClickGet Started, then clickCreate Policy.
21.SelectCreate Your Own Policy.
22.InPolicy Name, type
NoteCopy the name to your clipboard for later use.
23.Copy and paste the following policy document into thePolicy Documentbox. As you paste the code, review it. Can you tell what the policy is doing?
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:DescribeSecurityGroups",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:RevokeSecurityGroupIngress"
],
"Resource": "*"
}
]
}
24.ClickCreate Policy.
The policy you created provides permissions to the Lambda function to read the EC2 Security Groups and make necessary changes to their ingress rules. It also allows the Lambda function to write logs to the Cloudwatch Logs service.
Update IAM role
An IAM role was pre-created as part of the lab setup. The name of the role islambda-role. In this section, you'll be attaching the IAM policy created in the previous section tolambda-role.
25.In the AWS Management Console, clickServicesthen clickIAM.
26.In the navigation pane, clickRoles.
27.Click the role namedlambda-role.
28.ClickAttach Policy.
29.On theAttach Policyscreen, select policy you created earlier. You can search for the policy by enteringin the search filter.
30.ClickAttach Policy.
The role has now been updated with the required permissions needed for the Lambda function that will be setup in the next section.
Create the Lambda function
31.On theServicesmenu, clickLambda.
32.ClickCreate a Lambda function.
NoteIf you've never created Lambda functions before, you will clickGet Started Now. If you have existing Lambda functions, clickFunctionson the navigation pane.
33.You are prompted to select a blueprint. Blueprints can be a great starting point when you build your own Lambda function. However in this lab, the function code will be provided for you, so you should clickBlank Function.
34.You can skip configuring your trigger. On theConfigure Triggerspage, simply clickNext.
35.ForName, type. This is the function name.
36.ForRuntime, select Python 2.7.
37.ForCode entry type, clickEdit code inline. Remove the placeholder code and paste the following code:
'''
Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
'''
importboto3
importhashlib
importjson
importurllib2
# Name of the service, as seen in the ip-groups.json file, to extract information for
SERVICE="CLOUDFRONT"
# Ports your application uses that need inbound permissions from the service for
INGRESS_PORTS= [ 80 ]
# Tags which identify the security groups you want to update
44.After you've added the sample event, clickSave and test. Your Lambda function will be invoked, and the output will report an error in execution. You should see the log output at the bottom of the console similar to the following.
You will see a message indicating there was a hash mismatch. Normally, a real SNS notification from the IP Ranges SNS topic will include the right hash, but because our sample event is a test case representing the event, you will need to update the sample event manually to have the expected hash. Copy the hash value after the word "got" in the sentence "...gotHash Valueexpected 7fd59f5c7f5cf643036cbd4443ad3e4b". "errorMessage": "MD5 Mismatch: got 001fd33aa4135060111a137ae58cb057 expected 7fd59f5c7f5cf643036cbd4443ad3e4b" Use 001fd33aa4135060111a137ae58cb057
45.In the Lambda console, select your function, clickActions, and then clickConfigure test event. Replace themd5value in theMessagefield of the sample event with the hash value you copied in the previous step.
It should look something like this (note that this is just an example and yourmd5falue may be different):
"md5\": \"88386cb87e7814b75bc518eb841e92bb\",
46.ClickSave and test.
Your Lambda function will be invoked. This time, you should see a succesfull output indicating your security group was properly updated with the IP ranges belonging to Amazon CloudFront and AWS WAF.
Verify Security Group update
The Lambda function when tested would have updated the previously created security group with the latest IP ranges belonging to Amazon CloudFront and AWS WAF. To view and verify the update:
47.In the AWS Management Console, on theServicesmenu, clickEC2.
48.In the navigation pane, find theNETWORK & SECURITYheading, then clickSecurity Groups.
49.Select the security group created earlier,AutoUpdateSecurityGroup.
50.In the bottom pane, select theInboundtab.
You will now see all the CloudFront IP ranges added as allowed points of ingress.
Configure Lambda function's trigger
Subscribe the Lambda function to the SNS topic so that any changes in the IP ranges automatically gets updated on the security group's ingress rules.
51.In the AWS Management Console, chooseUS East (N. Virginia)as the region in top right corner.
ImportantEnsure that the region is set to US East (N. Virginia) before proceeding with the next step.
52.On theServicesmenu, clickSNS.
53.ClickGet Startedif that option is available on the page.
54.In the navigation pane, clickSubscriptions.
55.ClickCreate subscription.
56.ForTopic ARN, enter
57.ForProtocol, clickAWS Lambda.
58.ForEndpoint, choose the Lambda function you created earlier. You should see a function namedSecurityGroupAutoUpdate.
59.ForVersion or alias, select default.
60.ClickCreate subscription.
This subscription links the Lambda function that you created with the SNS topic so that any changes in the IP ranges when communicated over the SNS topic automatically invokes the fuction.
Verify SNS subscription as a Lambda trigger
61.Change the region back to where you created the Lambda function.
62.On theServicesmenu, clickLambda.
63.Select your Lambda function.
64.Select theTriggerstab.
65.Verify thatAmazonIPSpaceChangedis now a trigger for the Lambda function.
Conclusion
Congratulations! You have successfully created a Lambda function that gets triggered when AWS publishes service IP address updates. This subscription links the Lambda function that you created with the SNS topic so that any changes in the IP ranges when communicated over the SNS topic automatically invokes the fuction.
End Your Lab
Follow these steps to close the console, end your lab, and evaluate the experience.
66.In the upper right of the navigation bar of the AWS Management Console, clickyourqwiklabsacct@<AccountNumber>, and then clickSign Out.
67.Close any active SSH client sessions or remote desktop sessions.