Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I've been trying to use the AWS Update-LMFunctionCode to deploy my file to an existing lambda function in AWS.

Differing from the Publish-LMFunction where I can provide just a path to the zipFile (-FunctionZip), the Update-LMFunction wants a memorystream for its -Zipfile argument.

is there an example of loading a local zip file from disk into a memory stream that works? My initial calls are getting errors that the file can't be unzipped... 

$deployedFn =  Get-LMFunction -FunctionName $functionname

        "Function Exists - trying to update"

        try{

            [system.io.stream]$zipStream = [system.io.File]::OpenRead($zipFile)

        [byte[]]$filebytes = New-Object byte[] $zipStream.length

        [void] $zipStream.Read($filebytes, 0, $zipStream.Length)

            $zipStream.Close()

            "$($filebytes.length)"

        $zipString =  [System.Convert]::ToBase64String($filebytes)

        $ms = new-Object IO.MemoryStream

        $sw = new-Object IO.StreamWriter $ms

        $sw.Write($zipString)

        Update-LMFunctionCode -FunctionName $functionname -ZipFile $ms

            }

        catch{

             $ErrorMessage = $_.Exception.Message

            Write-Host $ErrorMessage

            break

        }

docs for the Powershell function is here: http://docs.aws.amazon.com/powershell/latest/reference/items/Update-LMFunctionCode.html although it wants to live in a frame...

1 Answer

0 votes
by (44.4k points)

To copy from one stream to another, use the CopyTo method:

try {

    $zipFilePath = "index.zip"

    $zipFileItem = Get-Item -Path $zipFilePath

    $fileStream = $zipFileItem.OpenRead()

    $memoryStream = New-Object System.IO.MemoryStream

    $fileStream.CopyTo($memoryStream)

 

    Update-LMFunctionCode -FunctionName "PSDeployed" -ZipFile $memoryStream

}

finally {

    $fileStream.Close()

}

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...