- [Oksana] Have you ever thought how media files travel over the internet when you open a website? How do they appear instantly? And in some cases, why does it take so long for them to load? In fact, there are many reasons for network latency. You might experience poor internet connectivity because of rain and other natural conditions that disturb wireless signals. The DNS server in your area could be down and preventing your website from loading. You could also have hardware issues, such as your older router might no longer support the speed of the internet, or a server with your media files could reside too far from your users. While we can't solve all of these issues in our course, we can look into the most common one. If media files have to travel a long distance from the server to the client device, why don't we store those files closer to your end users? Well, yes, you keep media files in the geographic area where most of your users live. But what happens if you decide to expand your reach? With AWS, you can cache copies of your images in edge locations. These edge locations reside in many major cities. They are designed to store and deliver copies of your content to users nearby. Suppose your customer lives in Seattle, and you deployed an app and all of the resources in the US West Oregon Region. One day, your customer receives an invitation to visit a pet-adoption fundraiser in Paris. This is a great opportunity to demo the new app. However, you might have some concerns that it will take too long to load hundreds of images on the website, and you're right. Latency issues and website issues could make your app appear unprofessional. To reduce latency, create a content delivery network, or CDN. The CDN in Lightsail is backed by Amazon CloudFront, the main AWS service that distributes content across more than 300 edge locations to over 90 cities in 47 countries. With Lightsail CDN, when a user in France opens your app, the CDN checks if the data centers nearby have a copy of your app. If they don't, the CDN fetches app content from the origin, in our case, the origin is the bucket, and then caches content in edge locations close to where your request came from. This way, when users try to open your website, they access the cached copy of your app. To create a CDN distribution, in the Lightsail console, choose Networking and choose Create distribution. Then, choose the origin of your distribution. This could be an instance, a load balancer, a bucket, or a container. Choose a bucket to distribute pet images. Then, choose the pricing plan, and create a name for your distribution, or use the default name. Choose Create distribution. After the distribution status changes from In progress to Enabled, you can verify that it's working. In the Lightsail console, on the Instances tab, choose the IP address of your instance. The pet-adoption app appears. Open developer tools, or inspect the page. Choose the selection element and choose the image. As you noticed, the app doesn't use the distribution. How do we fix this? You need to update the configuration file again. This time, point the image URL to the distribution domain. Copy and save the distribution domain. Then, on the Lightsail instance, open the terminal icon, type cd stack/apache2/configs. Return, then type sudo nano config.php. Replace the image URL with the domain name of your distribution. At the beginning of the domain name, add https:// and add a / at the end of the URL. To exit the text editor, hold down Control key and press X, press Y, and Return. Now, refresh the browser window and inspect images again. The path of your images now points to the CloudFront distribution. Rest assured, your customer can confidently present the app at the fundraiser in Paris. What happens if the server experiences an outage? The server might be going through maintenance or updates, or it might be also receiving high volumes of traffic that could cause temporary downtime. To help avoid these issues, you can make your app highly available by spreading the app's workload between several instances. More about load balancing in our next video.