AWS CloudShell에 .NET 5를 설치하여 Windows용 실행 파일 만들기

17139 단어 C#.NETAWS

소개



AWS CloudShell은 AWS 관리 콘솔에서 Amazon Linux2를 시작하고 AWS의 각 기능을 명령줄 기반으로 실행하는 기능입니다. Amazon Linux2는 CentOS7에 가까운 환경이므로 yum을 사용하여 .NET Core를 설치할 수 있습니다.

이번에는 AWS CloudShell에 .NET 5를 설치하여 앱을 빌드 → 실행 → Windows용 실행 파일을 다운로드해 봅니다.
# 누가 기쁘다! ? 라는 내용이지만,,,

CloudShell 시작 및 .NET Core5 설치



AWS Management Console에 로그인하고 >_ 아이콘을 클릭하여 CloudShell을 시작합니다.


그리고는 CentOS에 .NET SDK 또는 .NET 런타임 설치 의, CentOS7 에 대한 순서를 실행해 설정해 갑니다.
먼저 리포지토리에 Microsoft 리포지토리를 추가합니다.

CloudShell
$ sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
Retrieving https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
warning: waiting for transaction lock on /var/lib/rpm/.rpm.lock
Preparing...                          ################################# [100%]
Updating / installing...
   1:packages-microsoft-prod-1.0-1    ################################# [100%]

설치를 실행하면 실제로 패키지를 다운로드하는 타이밍과 설치 전에 확인을 요구하므로 둘 다 y 로 응답하면 설치가 완료됩니다.

CloudShell
$  sudo yum install dotnet-sdk-5.0
Loaded plugins: ovl, priorities
packages-microsoft-com-prod                                               | 3.0 kB  00:00:00     
packages-microsoft-com-prod/primary_db                                    | 278 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package dotnet-sdk-5.0.x86_64 0:5.0.101-1 will be installed
--> Processing Dependency: netstandard-targeting-pack-2.1 >= 2.1.0 for package: dotnet-sdk-5.0-5.0.101-1.x86_64
--> Processing Dependency: dotnet-runtime-5.0 for package: dotnet-sdk-5.0-5.0.101-1.x86_64
--> Processing Dependency: dotnet-targeting-pack-5.0 for package: dotnet-sdk-5.0-5.0.101-1.x86_64
--> Processing Dependency: dotnet-apphost-pack-5.0 for package: dotnet-sdk-5.0-5.0.101-1.x86_64
--> Processing Dependency: aspnetcore-runtime-5.0 for package: dotnet-sdk-5.0-5.0.101-1.x86_64
--> Processing Dependency: aspnetcore-targeting-pack-5.0 for package: dotnet-sdk-5.0-5.0.101-1.x86_64
--> Running transaction check
---> Package aspnetcore-runtime-5.0.x86_64 0:5.0.1-1 will be installed
---> Package aspnetcore-targeting-pack-5.0.x86_64 0:5.0.0-1 will be installed
---> Package dotnet-apphost-pack-5.0.x86_64 0:5.0.1-1 will be installed
---> Package dotnet-runtime-5.0.x86_64 0:5.0.1-1 will be installed
--> Processing Dependency: dotnet-hostfxr-5.0 >= 5.0.1 for package: dotnet-runtime-5.0-5.0.1-1.x86_64
--> Processing Dependency: dotnet-runtime-deps-5.0 >= 5.0.1 for package: dotnet-runtime-5.0-5.0.1-1.x86_64
---> Package dotnet-targeting-pack-5.0.x86_64 0:5.0.0-1 will be installed
---> Package netstandard-targeting-pack-2.1.x86_64 0:2.1.0-1 will be installed
--> Running transaction check
---> Package dotnet-hostfxr-5.0.x86_64 0:5.0.1-1 will be installed
--> Processing Dependency: dotnet-host >= 5.0.1 for package: dotnet-hostfxr-5.0-5.0.1-1.x86_64
---> Package dotnet-runtime-deps-5.0.x86_64 0:5.0.1-1 will be installed
--> Running transaction check
---> Package dotnet-host.x86_64 0:5.0.1-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================
 Package                           Arch      Version        Repository                      Size
=================================================================================================
Installing:
 dotnet-sdk-5.0                    x86_64    5.0.101-1      packages-microsoft-com-prod     80 M
Installing for dependencies:
 aspnetcore-runtime-5.0            x86_64    5.0.1-1        packages-microsoft-com-prod    8.0 M
 aspnetcore-targeting-pack-5.0     x86_64    5.0.0-1        packages-microsoft-com-prod    2.1 M
 dotnet-apphost-pack-5.0           x86_64    5.0.1-1        packages-microsoft-com-prod    4.6 M
 dotnet-host                       x86_64    5.0.1-1        packages-microsoft-com-prod     64 k
 dotnet-hostfxr-5.0                x86_64    5.0.1-1        packages-microsoft-com-prod    170 k
 dotnet-runtime-5.0                x86_64    5.0.1-1        packages-microsoft-com-prod     29 M
 dotnet-runtime-deps-5.0           x86_64    5.0.1-1        packages-microsoft-com-prod    2.8 k
 dotnet-targeting-pack-5.0         x86_64    5.0.0-1        packages-microsoft-com-prod    3.1 M
 netstandard-targeting-pack-2.1    x86_64    2.1.0-1        packages-microsoft-com-prod    2.1 M

Transaction Summary
=================================================================================================
Install  1 Package (+9 Dependent packages)

Total download size: 129 M
Installed size: 369 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/2/packages-microsoft-com-prod/packages/aspnetcore-targeting-pack-5.0.0.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Public key for aspnetcore-targeting-pack-5.0.0.rpm is not installed
(1/10): aspnetcore-targeting-pack-5.0.0.rpm                               | 2.1 MB  00:00:00     
(2/10): dotnet-apphost-pack-5.0.1-x64.rpm                                 | 4.6 MB  00:00:00     
(3/10): dotnet-host-5.0.1-x64.rpm                                         |  64 kB  00:00:00     
(4/10): dotnet-hostfxr-5.0.1-x64.rpm                                      | 170 kB  00:00:00     
(5/10): aspnetcore-runtime-5.0.1-x64.rpm                                  | 8.0 MB  00:00:01     
(6/10): dotnet-runtime-deps-5.0.1-centos.7-x64.rpm                        | 2.8 kB  00:00:00     
(7/10): dotnet-runtime-5.0.1-x64.rpm                                      |  29 MB  00:00:01     
(8/10): dotnet-targeting-pack-5.0.0-x64.rpm                               | 3.1 MB  00:00:00     
(9/10): netstandard-targeting-pack-2.1.0-x64.rpm                          | 2.1 MB  00:00:00     
(10/10): dotnet-sdk-5.0.101-x64.rpm                                       |  80 MB  00:00:09     
-------------------------------------------------------------------------------------------------
Total                                                             11 MB/s | 129 MB  00:00:11     
Retrieving key from https://packages.microsoft.com/keys/microsoft.asc
Importing GPG key 0xBE1229CF:
 Userid     : "Microsoft (Release signing) <[email protected]>"
 Fingerprint: bc52 8686 b50d 79e3 39d3 721c eb3e 94ad be12 29cf
 From       : https://packages.microsoft.com/keys/microsoft.asc
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : dotnet-targeting-pack-5.0-5.0.0-1.x86_64                                     1/10 
  Installing : aspnetcore-targeting-pack-5.0-5.0.0-1.x86_64                                 2/10 
  Installing : dotnet-host-5.0.1-1.x86_64                                                   3/10 
  Installing : dotnet-hostfxr-5.0-5.0.1-1.x86_64                                            4/10 
  Installing : dotnet-runtime-deps-5.0-5.0.1-1.x86_64                                       5/10 
  Installing : dotnet-runtime-5.0-5.0.1-1.x86_64                                            6/10 
  Installing : aspnetcore-runtime-5.0-5.0.1-1.x86_64                                        7/10 
  Installing : netstandard-targeting-pack-2.1-2.1.0-1.x86_64                                8/10 
  Installing : dotnet-apphost-pack-5.0-5.0.1-1.x86_64                                       9/10 
  Installing : dotnet-sdk-5.0-5.0.101-1.x86_64                                             10/10 
This software may collect information about you and your use of the software, and send that to Microsoft.
Please visit http://aka.ms/dotnet-cli-eula for more information.
Welcome to .NET!
---------------------
Learn more about .NET: https://aka.ms/dotnet-docs
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

Configuring...
--------------
A command is running to populate your local package cache to improve restore speed and enable offline access. This command takes up to one minute to complete and only runs once.
  Verifying  : dotnet-hostfxr-5.0-5.0.1-1.x86_64                                            1/10 
  Verifying  : dotnet-apphost-pack-5.0-5.0.1-1.x86_64                                       2/10 
  Verifying  : aspnetcore-targeting-pack-5.0-5.0.0-1.x86_64                                 3/10 
  Verifying  : netstandard-targeting-pack-2.1-2.1.0-1.x86_64                                4/10 
  Verifying  : dotnet-runtime-5.0-5.0.1-1.x86_64                                            5/10 
  Verifying  : aspnetcore-runtime-5.0-5.0.1-1.x86_64                                        6/10 
  Verifying  : dotnet-targeting-pack-5.0-5.0.0-1.x86_64                                     7/10 
  Verifying  : dotnet-sdk-5.0-5.0.101-1.x86_64                                              8/10 
  Verifying  : dotnet-runtime-deps-5.0-5.0.1-1.x86_64                                       9/10 
  Verifying  : dotnet-host-5.0.1-1.x86_64                                                  10/10 

Installed:
  dotnet-sdk-5.0.x86_64 0:5.0.101-1                                                              

Dependency Installed:
  aspnetcore-runtime-5.0.x86_64 0:5.0.1-1         aspnetcore-targeting-pack-5.0.x86_64 0:5.0.0-1
  dotnet-apphost-pack-5.0.x86_64 0:5.0.1-1        dotnet-host.x86_64 0:5.0.1-1                  
  dotnet-hostfxr-5.0.x86_64 0:5.0.1-1             dotnet-runtime-5.0.x86_64 0:5.0.1-1           
  dotnet-runtime-deps-5.0.x86_64 0:5.0.1-1        dotnet-targeting-pack-5.0.x86_64 0:5.0.0-1    
  netstandard-targeting-pack-2.1.x86_64 0:2.1.0-1

Complete!

.NET 5가 설치되었습니다.

CloudShell
$ dotnet --version
5.0.101

빌드하여 Windows용 실행 파일 다운로드



Console 앱을 만들고 실행해 봅니다.

CloudShell
$ mkdir consoleapp
$ cd consoleapp
$ dotnet new console
$ dotnet run
Hello World!

Program.cs를 열고

CloudShell
$ ls
bin  obj  Program.cs  sample.csproj
$ vi Program.cs 

글쎄, 자주 수정하고 저장 한 후,

Program.cs
using System;

namespace sample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello CloudShell");
        }
    }
}

빌드하고 실행하면 반영되었습니다.

CloudShell
$ dotnet run
Hello CloudShell

Windows용으로 빌드합니다.

CloudShell
$ dotnet publish -r win-x86 -c Release -p:PublishSingleFile=true
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  sample -> /home/cloudshell-user/sample/bin/Release/net5.0/win-x86/sample.dll
  sample -> /home/cloudshell-user/sample/bin/Release/net5.0/win-x86/publish/

완성된 파일을 tar로 굳혀 다운로드합니다.

CloudShell
$ cd /home/cloudshell-user/sample/bin/Release/net5.0/win-x86/
$ tar cvfz ~/app.tar.gz publish/
publish/
publish/sample.exe
publish/mscordaccore.dll
publish/clrjit.dll
publish/clrcompression.dll
publish/sample.pdb
publish/coreclr.dll

Actions → Download file에서 파일을 다운로드할 수 있습니다.



Windows에서 실행해 봅니다.

움직였습니다!

요약


  • AWS CloudShell에서도 넣으면 .NET 움직일거야
  • .NET 크로스 컴파일러에서 Windows, Mac 실행 파일을 만들 수 있습니다
  • 일부 빌드 방법은 .NET 런타임이 설치되어 있지 않아도 작동합니다.

    CloudShell에서는 영속화되는 것은 Home 디렉토리 부하의 1GB의 파일만으로, 다른 것은 기동할 때마다 없어지므로 처음부터 인스톨 되고 있는 런타임을 사용하는 편이 좋네요. PowerShell Core도 포함되어 있습니다.
  • 좋은 웹페이지 즐겨찾기