avatarTeri Radichel

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

7000

Abstract

</b></p><p id="8424">The CloudWatch service is a log repository where you can store all the logs created by your applications and infrastructure if you configure them to send logs to this repository. It also has some monitoring dashboards to help you monitor your infrastructure. In an earlier post we configured VPC Flow Logs to send logs to CloudWatch.</p><div id="7f96" class="link-block"> <a href="https://readmedium.com/vpc-flow-logs-governance-1f0790ad29ec"> <div> <div> <h2>VPC Flow Logs Governance</h2> <div><h3>ACM.63 Enforce the existence of VPC Flow Logs on All VPCs</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*UMlMQi6ZexHzw-m4Lx-3Gw.png)"></div> </div> </div> </a> </div><p id="2955">Most services will also let you send logs to an S3 bucket but sometimes it’s easier to query CloudWatch. But CloudWatch costs more than S3.</p><p id="9b73">By default, Lambda should be sending logs to CloudWatch but when I head over there, the logs don’t exist. Sometimes it takes a while for the logs to show up but in this case the logs never appear.</p><figure id="c509"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*NnDNysKpMa1sq1ZLc4zpAA.png"><figcaption>Q</figcaption></figure><p id="e7c7">Why not?</p><blockquote id="b9b4"><p>Your execution role needs permission to upload logs to CloudWatch Logs.</p></blockquote><div id="d8eb" class="link-block"> <a href="https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html"> <div> <div> <h2>Accessing Amazon CloudWatch logs for AWS Lambda</h2> <div><h3>AWS Lambda automatically monitors Lambda functions on your behalf, pushing logs to Amazon CloudWatch. To help you…</h3></div> <div><p>docs.aws.amazon.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><p id="b023">We need to add that to our Lambda IAM policy created in the last post. It is unfortunate that the documentation refers to a managed policy alone instead of explicitly telling us which permissions we require. #awswishlist.</p><p id="08c3">We can look at the AWS Managed policy <b>AWSLambdaBasicExecutionRole</b> in the IAM console.</p><figure id="8c24"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MJRoSyAJRSYmAJS9WPfLjA.png"><figcaption></figcaption></figure><p id="134d">Let’s add those actions to our AWS policy.</p><figure id="02c2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*IPhmvsX2M2D2Y1EJUxg0Pw.png"><figcaption></figcaption></figure><p id="2f3c">Run the Lambda deploy script from the last post and verify the policy gets updated to include the above actions.</p><p id="4428">Now click that Test button again. Head over to CloudWatch logs.</p><p id="c7ab">As you can see now I have what is called a <b>Log Group</b> created by <b>Lambda</b> in <b>CloudWatch </b>called /aws/lambda/dockertest. Remember that dockertest was the name of the Lambda function. Within the log group there are <b>Log streams. </b>Each log stream contains a number of messages collected up to a certain amount or time.</p><figure id="4218"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*AYbDdyzpVPvnXJOpIO2T9g.png"><figcaption></figcaption></figure><p id="6b43">Now that we know what the format is for our LogGroup, how can we improve our Lambda function policy? It’s up to AWS to ensure logs cannot be written to the wrong log stream because they own that code. Still, we can improve our policy by limiting our Lambda Function to only logging to its own log stream like this:</p><figure id="67b4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*R1ThKA4gRnKaMOHcKpsgeA.png"><figcaption></figcaption></figure><p id="1634">When I try to deploy that, it doesn’t work. It says the resource needs to be in an ARN format. The ARN is on the details page of the log group:</p><figure id="71c4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Ge4waVoOSpCTYniZj5vYLA.png"><figcaption></figcaption></figure><p id="1990">Let’s try that again:</p><figure id="fd13"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5qDQEpvGo85diVUesjTW3A.png"><figcaption></figcaption></figure><p id="389a">When I look at the summary in the AWS console, the interpretation of the allowed resources is incorrect. I hope this is not actually how this policy is getting evaluated behind the scenes. It should be limiting writing logs to only the specified Log group with the name matching what is specified in the policy above.</p><figure id="cc48"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nQS_reVgzQkjUPu90_lUow.png"><figcaption></figcaption></figure><p id="d30d">A way to test that would be to add some code to the Lambda function that tries to log to some other log stream but I’m not going to go into all that right now. I’m sure someone else will for fame and glory if this is, in fact, not correctly limiting logging to the specified log stream in the policy.</p><p id="752c">At any rate, now we can test to see if our Lambda function is still writing to CloudWatch logs. To be absolutely sure everything still works, I delete the existing log group created by Lambda above.</p><figure id="7b76"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VtU7ymwurbJo0jIxyx04SA.png"><figcaption></figcaption></figure><p id="e8f6">I test the Lambda function again, and I do get logs in CloudWatch. So if this is working correctly in the AWS IAM Policy evaluation logic, then my Lambda function should only be able to write to its own log stream.</p><p id="0cd6">As I’ve shown in a prior post, you can write information out to these CloudWatch logs from a Lambda function. The above policy should prevent stealthy exfiltration where someone tries to write logs to some alternate location with the Lambda function secret I am getting to, for example. The standard templates help you easily correlate your logs with your Lambda functions.</p><p id="23f2">There are a number of ways someone can try to exfiltrate sensitive or secret data using AWS logs so ensuring your log permissions are set up correctly and checking logs written by applications to make sure no secrets get written to logs is important. In fact, I leverage logs to steal a token in this RSA talk I keep referring back to because it demonstrates so many issues. See the demo at the end.</p> <figure id="685f"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" s

Options

rc="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FZv2jFISTHuE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZv2jFISTHuE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FZv2jFISTHuE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854"> </div> </div> </figure></iframe></div></div></figure><h2 id="d153">CloudWatch Log Structure for Lambda</h2><p id="db98">You can read more about the log structure here:</p><figure id="1057"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*YMa7H7tCuQqiTmwc7eIUEA.png"><figcaption></figcaption></figure><div id="dfab" class="link-block"> <a href="https://docs.aws.amazon.com/lambda/latest/operatorguide/log-structure.html"> <div> <div> <h2>How CloudWatch structures logs</h2> <div><h3>Lambda automatically streams details about each function invocation, along with logs and other output from your…</h3></div> <div><p>docs.aws.amazon.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*jZ6sW-px-aJKwSHX)"></div> </div> </div> </a> </div><p id="6156"><b>CloudWatch Metrics</b></p><p id="af9b">You can also check various <b>metrics</b> in CloudWatch. Let’s say you are trying to see how many times a Lambda function was invoked and how many times it failed:</p><figure id="ab90"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*j-l_upC6tRyE920lyJxo9w.png"><figcaption></figcaption></figure><p id="1375">More about CloudWatch metrics here and a list of available metrics:</p><div id="8407" class="link-block"> <a href="https://docs.aws.amazon.com/lambda/latest/operatorguide/important-metrics.html"> <div> <div> <h2>Important metrics for CloudWatch</h2> <div><h3>Lambda reports some metrics directly to the CloudWatch service and these do not appear in the logs. </h3></div> <div><p>docs.aws.amazon.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><p id="13e9">You can also create custom metrics.</p><div id="e902" class="link-block"> <a href="https://docs.aws.amazon.com/lambda/latest/operatorguide/custom-metrics.html"> <div> <div> <h2>Custom metrics</h2> <div><h3>CloudWatch can also track custom metrics that are application-specific. By default, metrics from AWS services are…</h3></div> <div><p>docs.aws.amazon.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*ZCNSv-2uO1IQ552L)"></div> </div> </div> </a> </div><p id="6866">You can find logs and metrics on the Monitoring tab on the Lambda dashboard. I’ll explain what traces are later.</p><figure id="42f3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*LzWw7F97t79VemxoD-vLDg.png"><figcaption></figcaption></figure><p id="2c33"><b>CloudWatch Cost</b></p><p id="b9e0">Evaluate the cost up front when using these CloudWatch features. The AWS cost calculator can help, or a spreadsheet based on the CloudWatch pricing page, but I like to perform beta tests to really understand costs before deploying to production.</p><div id="0238" class="link-block"> <a href="https://calculator.aws/#/"> <div> <div> <h2>AWS Pricing Calculator</h2> <div><h3>AWS Pricing Calculator lets you explore AWS services, and create an estimate for the cost of your use cases on AWS.</h3></div> <div><p>calculator.aws</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><div id="1611" class="link-block"> <a href="https://aws.amazon.com/cloudwatch/pricing/"> <div> <div> <h2>Amazon CloudWatch Pricing - Amazon Web Services (AWS)</h2> <div><h3>undefined</h3></div> <div><p>undefined</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*Mrd1H_30Z1BVe-yx)"></div> </div> </div> </a> </div><p id="afa5">OK, we’ve got logs. Let’s move on.</p><p id="c76e">Follow for updates.</p><p id="01f7">Teri Radichel | <i>© <a href="https://2ndsightlab.com/?source=post_page---------------------------">2nd Sight Lab</a> 2024</i></p><div id="8334"><pre><span class="hljs-section">About Teri Radichel:

⭐️ Author: Cybersecurity for Executives in the Age of Cloud
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight Lab</pre></div><div id="46f6"><pre><span class="hljs-section">Need Help With Cybersecurity, Cloud, or Appication Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for Presenation</pre></div><div id="5a42"><pre><span class="hljs-attribute">Follow for more stories like this</span><span class="hljs-punctuation">:</span>
<span class="hljs-attribute">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
❤️ Medium</span><span class="hljs-punctuation">:</span> <span class="hljs-string">Teri Radichel</span>
<span class="hljs-attribute">❤️ Sign Up For Email
❤️ Twitter</span><span class="hljs-punctuation">:</span> <span class="hljs-string">@teriradichel</span>
<span class="hljs-attribute">❤️ Mastodon</span><span class="hljs-punctuation">:</span> <span class="hljs-string">@[email protected]</span>
<span class="hljs-attribute">❤️ Facebook</span><span class="hljs-punctuation">:</span> <span class="hljs-string">2nd Sight Lab</span>
<span class="hljs-attribute">❤️ YouTube</span><span class="hljs-punctuation">:</span> <span class="hljs-string">@2ndsightlab</span></pre></div><figure id="faf5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*H9Ew1KCl-29nZiPR.jpeg"><figcaption></figcaption></figure></article></body>

Testing a Lambda function with the AWS Console and Enabling CloudWatch Logs and Metrics

ACM.299 Restricting Lambda functions to write to their own CloudWatch logs groups

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

⚙️ Check out my series on Automating Cybersecurity Metrics | Code.

🔒 Related Stories: Lambda | Container Security | Application Security

💻 Free Content on Jobs in Cybersecurity | ✉️ Sign up for the Email List

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~In the last post I deployed a Lambda function with a container.

In this post I want to test that container to see if it works. Since I copied a Dockerfile from an AWS example, I hope it will just run but spoiler alert — it doesn’t. Before we address that issue, I am going to show how to fix the IAM Policy to allow logging to CloudWatch.

A Lambda function is a little chunk of executable code that you can run without deploying servers and so on. I am trying to run code in a container created with a Dockerfile provided in an AWS example. I deployed a Lambda function using that container in the last post.

But how do we trigger or run it? Let’s say you’re on your laptop outside of AWS.

You can log into the console and run the Lambda function.

What’s actually happening is that the AWS Console is calling an AWS API on your behalf to invoke the function when you click the button.

Essentially you can configure whatever inputs your Lambda requires and push a button. My Lambda function doesn’t require any inputs.

Navigate to the Lambda dashboard.

Click Functions on the left.

Click on your Function name.

Tip: I never find the Function overview useful. I always close it. I don’t know if other people find this useful. Maybe in complex architectures. But I wish there was a way to permanently close or disable it. #awswishlist

Click on Test.

There’s a test event preconfigured. I’m just going to leave that as is. The Lambda function I’m running will simply ignore those inputs.

Click Test.

Failed. Click the down arrow to view the details. Well, that’s interesting. But that’s not what I’m going to fix in this post. I want to show you how to fix logging to CloudWatch so hold this thought.

Click on the links that should take you to the logs. If you were executing this function outside the AWS Console, you would want to be able to review the logs to see what went wrong.

That link takes you over to a service called CloudWatch.

CloudWatch Logs

The CloudWatch service is a log repository where you can store all the logs created by your applications and infrastructure if you configure them to send logs to this repository. It also has some monitoring dashboards to help you monitor your infrastructure. In an earlier post we configured VPC Flow Logs to send logs to CloudWatch.

Most services will also let you send logs to an S3 bucket but sometimes it’s easier to query CloudWatch. But CloudWatch costs more than S3.

By default, Lambda should be sending logs to CloudWatch but when I head over there, the logs don’t exist. Sometimes it takes a while for the logs to show up but in this case the logs never appear.

Q

Why not?

Your execution role needs permission to upload logs to CloudWatch Logs.

We need to add that to our Lambda IAM policy created in the last post. It is unfortunate that the documentation refers to a managed policy alone instead of explicitly telling us which permissions we require. #awswishlist.

We can look at the AWS Managed policy AWSLambdaBasicExecutionRole in the IAM console.

Let’s add those actions to our AWS policy.

Run the Lambda deploy script from the last post and verify the policy gets updated to include the above actions.

Now click that Test button again. Head over to CloudWatch logs.

As you can see now I have what is called a Log Group created by Lambda in CloudWatch called /aws/lambda/dockertest. Remember that dockertest was the name of the Lambda function. Within the log group there are Log streams. Each log stream contains a number of messages collected up to a certain amount or time.

Now that we know what the format is for our LogGroup, how can we improve our Lambda function policy? It’s up to AWS to ensure logs cannot be written to the wrong log stream because they own that code. Still, we can improve our policy by limiting our Lambda Function to only logging to its own log stream like this:

When I try to deploy that, it doesn’t work. It says the resource needs to be in an ARN format. The ARN is on the details page of the log group:

Let’s try that again:

When I look at the summary in the AWS console, the interpretation of the allowed resources is incorrect. I hope this is not actually how this policy is getting evaluated behind the scenes. It should be limiting writing logs to only the specified Log group with the name matching what is specified in the policy above.

A way to test that would be to add some code to the Lambda function that tries to log to some other log stream but I’m not going to go into all that right now. I’m sure someone else will for fame and glory if this is, in fact, not correctly limiting logging to the specified log stream in the policy.

At any rate, now we can test to see if our Lambda function is still writing to CloudWatch logs. To be absolutely sure everything still works, I delete the existing log group created by Lambda above.

I test the Lambda function again, and I do get logs in CloudWatch. So if this is working correctly in the AWS IAM Policy evaluation logic, then my Lambda function should only be able to write to its own log stream.

As I’ve shown in a prior post, you can write information out to these CloudWatch logs from a Lambda function. The above policy should prevent stealthy exfiltration where someone tries to write logs to some alternate location with the Lambda function secret I am getting to, for example. The standard templates help you easily correlate your logs with your Lambda functions.

There are a number of ways someone can try to exfiltrate sensitive or secret data using AWS logs so ensuring your log permissions are set up correctly and checking logs written by applications to make sure no secrets get written to logs is important. In fact, I leverage logs to steal a token in this RSA talk I keep referring back to because it demonstrates so many issues. See the demo at the end.

CloudWatch Log Structure for Lambda

You can read more about the log structure here:

CloudWatch Metrics

You can also check various metrics in CloudWatch. Let’s say you are trying to see how many times a Lambda function was invoked and how many times it failed:

More about CloudWatch metrics here and a list of available metrics:

You can also create custom metrics.

You can find logs and metrics on the Monitoring tab on the Lambda dashboard. I’ll explain what traces are later.

CloudWatch Cost

Evaluate the cost up front when using these CloudWatch features. The AWS cost calculator can help, or a spreadsheet based on the CloudWatch pricing page, but I like to perform beta tests to really understand costs before deploying to production.

OK, we’ve got logs. Let’s move on.

Follow for updates.

Teri Radichel | © 2nd Sight Lab 2024

About Teri Radichel:
~~~~~~~~~~~~~~~~~~~~
⭐️ Author: Cybersecurity for Executives in the Age of Cloud
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight Lab
Need Help With Cybersecurity, Cloud, or Appication Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for Presenation
Follow for more stories like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
❤️ Medium: Teri Radichel
❤️ Sign Up For Email
❤️ Twitter: @teriradichel
❤️ Mastodon: @[email protected]
❤️ Facebook: 2nd Sight Lab
❤️ YouTube: @2ndsightlab
AWS
Lambda
Cloudwatch
Logs
Policy
Recommended from ReadMedium